如何检查每行中是否有任何按钮被选中

时间:2015-12-14 01:39:27

标签: javascript php jquery html

假设有3行,因此有3个不同的单选按钮名称,每个名称都有另外5个不同值的单选按钮。

代码:

   A          B            C
apple      ball         allbad-cat
apple      ball         allgood-car
dog        bark         allbad-pet
                        bull
                        dull
dog        bark         allgood-pet   

2 个答案:

答案 0 :(得分:1)

我正在考虑这样的事情:

  $("button").click(function() {
    var $rows = $("table tr");
    var $checked = $rows.find("input:checked");
    var remaining = $rows.length - $checked.length;
    if(remaining > 0)
      alert("answer all the questions: " + remaining + " remaining")
    else
      alert("sending info")
  });



$(function() {
  $("button").click(function() {
    var $rows = $("table tr");
    var $checked = $rows.find("input:checked");
    var remaining = $rows.length - $checked.length;
    if(remaining > 0)
      alert("answer all the questions: " + remaining + " remaining")
    else
      alert("sending info")
  });
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
	<td width="19">1</td>
	<td colspan="4"> question</td>
	<td width="42"><Input type="Radio" id="1" Name="point1" value="1"  />1</td>
	<td width="42"><Input type="Radio" id="2" Name="point1" value="2"  />2</td>
	<td width="42"><Input type="Radio" id="3" Name="point1" value="3"  />3</td>
	<td width="42"><Input type="Radio" id="4" Name="point1" value="4"  />4</td>
	<td width="42"><Input type="Radio" id="5" Name="point1" value="5"  />5</td>
	<br />
  </tr>
  <tr>
	<td width="19">2</td>
	<td colspan="4"> question</td>
	<td width="42"><Input type="Radio" id="1" Name="point2" value="1"  />1</td>
	<td width="42"><Input type="Radio" id="2" Name="point2" value="2"  />2</td>
	<td width="42"><Input type="Radio" id="3" Name="point2" value="3"  />3</td>
	<td width="42"><Input type="Radio" id="4" Name="point2" value="4"  />4</td>
	<td width="42"><Input type="Radio" id="5" Name="point2" value="5"  />5</td>
	<br />
  </tr>
  <tr>
	<td width="19">3</td>
	<td colspan="4"> question</td>
	<td width="42"><Input type="Radio" id="1" Name="point3" value="1"  />1</td>
	<td width="42"><Input type="Radio" id="2" Name="point3" value="2"  />2</td>
	<td width="42"><Input type="Radio" id="3" Name="point3" value="3"  />3</td>
	<td width="42"><Input type="Radio" id="4" Name="point3" value="4"  />4</td>
	<td width="42"><Input type="Radio" id="5" Name="point3" value="5"  />5</td>
	<br />
  </tr>
</table>
<button>submit</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<script language="javascript">
$(function() {
 $("button").click(function() {
   var $rows = $("table tr");
   var $checked = $rows.find("input:checked");
   var remaining = $rows.length - $checked.length;
   if(remaining > 0)
     alert("answer all the questions: " + remaining + " remaining")
       else
     alert("sending info")
  });
})