选择多个单选按钮(通过jquery,javascript?)

选择多个单选按钮(通过jquery,javascript?)

问题描述:

I currently have a form with lets say 5 radio button entries (see below). What im looking archive is the following: - Being able to choose multiple radio buttons - lets say 3 and submit the form.

Currently I got it working fine with PHP, SQL, but im only able to choose one radiobutton and submit that.

I figure it would also come in handy being able to deselect a radio button in case you wrongly click one.

My guess is that this can be done through some javascript? Any suggestions? Online examples perhaps?

<form id="pollform" action="poll.php" method="post">
<input id="option-1" type="radio" value="1" name="poll">
<label for="option-1">Select option 1</label>

<input id="option-2" type="radio" value="2" name="poll">
<label for="option-2">Select option 2</label>

<input id="option-3" type="radio" value="3" name="poll">
<label for="option-3">Select option 3</label>

<input id="option-4" type="radio" value="4" name="poll">
<label for="option-4">Select option 4</label>

<input id="option-5" type="radio" value="5" name="poll">
<label for="option-5">Select option 5</label>
</form>

我目前有一个表格,可以说5个单选按钮条目(见下文)。 什么看起来存档是 以下: -能够选择多个单选按钮 - 让我们说3并提交表单。 p>

目前我使用PHP,SQL正常工作,但我只能选择一个 radiobutton并提交。 p>

我认为如果您错误地点击一个单选按钮,它也会派上用场。 p>

我的 猜测这可以通过一些JavaScript来完成吗? 有什么建议? 在线示例可能吗? p>

 &lt; form id =“pollform”action =“poll.php”method =“post”&gt; 
&lt; input id =“option-1  “type =”radio“value =”1“name =”poll“&gt; 
&lt; label for =”option-1“&gt;选择选项1&lt; / label&gt; 
 
&lt; input id =”option-2  “type =”radio“value =”2“name =”poll“&gt; 
&lt; label for =”option-2“&gt;选择选项2&lt; / label&gt; 
 
&lt; input id =”option-3  “type =”radio“value =”3“name =”poll“&gt; 
&lt; label for =”option-3“&gt;选择选项3&lt; / label&gt; 
 
&lt; input id =”option-4  “type =”radio“value =”4“name =”poll“&gt; 
&lt; label for =”option-4“&gt;选择选项4&lt; / label&gt; 
 
&lt; input id =”option-5  “type =”radio“value =”5“name =”poll“&gt; 
&lt; label for =”option-5“&gt;选择选项5&lt; / label&gt; 
&lt; / form&gt; 
  code>   pre> 
  div>

As it has same name poll you will not be able to do that as input type radio is specialized in selecting a single value from multiple inputs.

You can use input type checkbox for that and make them as an array:

<form id="pollform" action="poll.php" method="post">
    <input id="option-1" type="checkbox" value="1" name="poll[]">
    <label for="option-1">Select option 1</label>

    <input id="option-2" type="checkbox" value="2" name="poll[]">
    <label for="option-2">Select option 2</label>

    <input id="option-3" type="checkbox" value="3" name="poll[]">
    <label for="option-3">Select option 3</label>

    <input id="option-4" type="checkbox" value="4" name="poll[]">
    <label for="option-4">Select option 4</label>

    <input id="option-5" type="checkbox" value="5" name="poll[]">
    <label for="option-5">Select option 5</label>
</form>

LIMIT (with jQuery) the number:

$("input[type=checkbox][name=poll[]]").click(function() {
    var numberSel = $("input[type=checkbox][name=poll[]]:checked").length >= 3;     
    $("input[type=checkbox][name=poll[]]").not(":checked").attr("disabled",numberSel);
});

Radio buttons are designed so that only one option from each group (as designated by their shared name) can be selected at once (just like you can only tune a radio to one station).

The input control which allows any number of options to be selected is the checkbox. If you append [] to their names, then the selected options will arrive on the PHP side as an array.

<input type="checkbox" value="1" name="poll[]" />
<input type="checkbox" value="2" name="poll[]" />

Radio buttons are designed for only 1 item to be selected, use checkboxes instead.