2014-03-26 96 views
0

我在这里提取的卷号在'php'中有两个单选按钮,将有多个卷号,所以我想检查所有单选按钮的值是'是'立刻使用特定的单选按钮或复选框。尽管我没有在这段代码中编写该按钮,因为我不知道该写什么。请给我一个使用java脚本的解决方案。使用一个单选按钮检查所有其他单选按钮

while($row=mysqli_fetch_assoc($result)) 
{ 

    echo"<tr><td>{$row['roll']}</td> 
    </td><td></td><td></td><td></td><td> 
    <td><input type='radio' name='present[$i]' value='Yes'>YES</td> 
      </td><td></td><td></td><td> 
      <td><input type='radio' name='present[$i]' value='No'>NO</td></tr>"; 
      $i++; 

} 
+0

你应该使用JavaScript来做到这一点。 –

回答

0

这种类型的交互应该使用Javascript实现,因为它是客户端操作。

HTML:

<form> 
    <input type="radio" name="radio1" value="yes"/>Yes 
    <input type="radio" name="radio1" value="no"/>No 
    <br /> 
    <input type="radio" name="radio2" value="yes"/>Yes 
    <input type="radio" name="radio2" value="no"/>No 
    <br /> 
    <input type="radio" name="radio3" value="yes"/>Yes 
    <input type="radio" name="radio3" value="no"/>No 
    <br /> 
    <input type="button" value="Select All" onclick="selectAll('radio',true);"/> 
    <input type="button" value="Deselect All" onclick="selectAll('radio',false);"/> 
</form> 

的Javascript:

function selectAll(prefix, set) { 
    var form = document.forms[0], //Get the appropriate form 
     i = 0, 
     radio; 
    while(radio = form[prefix + ++i]) //Loop through all named radio# elements 
     for(var j = 0; j < radio.length; j++) //Loop through each set of named radio buttons 
      if(radio[j].value == (set ? "yes" : "no")) //Selector based on value of set 
       radio[j].checked = true; //Check that radio button! 
} 

JSFiddle

+0

非常感谢。其工作 – user3405123

+0

它的工作正常,如果名称就像radio1。如果我想使用循环,那么名称将喜欢广播[$我]。如何使它适用于这种类型的名称? – user3405123

+0

但它**是**使用循环来检查名称。它的格式为'prefix + i'(其中'prefix'指字符串“radio”)在Javascript中。如果您正在讨论PHP方面和创建这些DOM元素,那么您将使用以下PHP代码'$ prefix。 $ i'表示像“radio1”这样的名称,其中'$ prefix =“radio”;'在这种情况下。 –