kei kay
kei kay

Reputation: 43

how to check if any of the button in each row is selected

Suppose there is 3 row,thus there is 3 different name of radio button and each of them have another 5 radio button with different value.

Code:

$i = 1;
    while ($row = mysql_fetch_assoc($result))
    {
    ?>
        <tr>
        <td width="19"><?php echo $i ?></td>
        <td colspan="4"> <?php echo $row["question"] ?></td>
        <?php

        echo '<td width="42"><Input type="Radio" id="1" Name="point' . $i . '" value="1"  />1</td>';
        echo '<td width="42"><Input type="Radio" id="2" Name="point' . $i . '" value="2"  />2</td>';
        echo '<td width="42"><Input type="Radio" id="3" Name="point' . $i . '" value="3"  />3</td>';
        echo '<td width="42"><Input type="Radio" id="4" Name="point' . $i . '" value="4"  />4</td>';
        echo '<td width="42"><Input type="Radio" id="5" Name="point' . $i . '" value="5"  />5</td>';
        ?>
        <br />
        </tr>
    <?php 
    $i++;}

Upvotes: 3

Views: 55

Answers (2)

kei kay
kei kay

Reputation: 43

<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")
  });
})

Upvotes: 0

Santiago Hern&#225;ndez
Santiago Hern&#225;ndez

Reputation: 5636

I'm thinking some like this:

  $("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>

Upvotes: 1

Related Questions