简体   繁体   English

Javascript / HTML:单选按钮的Java脚本无法正常工作

[英]Javascript/HTML:Java script for Radio button not working properly

I have a problem with my java script and radio button. 我的Java脚本和单选按钮有问题。 I want to make radio button only can be choose only one in each row based on the id value from database, but in my case the function is working on the first row but the rest is not because it only return the first row value. 我想使单选按钮只能根据数据库中的id值在每一行中选择一个,但是在我的情况下,该函数在第一行上起作用,而其余功能不是,因为它仅返回第一行值。 the radio button is generate dynamically(the count of radio button row is based on how many id in db). 单选按钮是动态生成的(单选按钮的行数基于db中的id数)。

below is my java script: 以下是我的Java脚本:

function test1(){
var tes = document.getElementById('max');
var zes = document.getElementById('max1');
    alert(tes.value)
   if(tes.checked){

      zes.checked = false;
      }
   }

function test2(){
var tes = document.getElementById('max');
var zes = document.getElementById('max1');
    alert(zes.value)
    if(zes.checked){

    tes.checked = false;
    }
}

This is my radio button code: 这是我的单选按钮代码:

 <td align = 'center'>
 <?php
        $test = $col['id'];
        $testing = "select training.* from training inner join rekod on training.id = rekod.id where rekod.id = '$test'";
        $userx = mysql_query($testing) or die (mysqli_error());
        $u = mysql_fetch_assoc($userx);
        echo $u['id'];
        echo "<input type ='radio'  name='x' onchange='test1();' required id='max' value ='".$u['id']."'>";?>

    </td><td align='center'>
    <?php
        $te = $col['id'];
        $tes = "select training.* from training inner join rekod on training.id = rekod.id where rekod.id = '$te'";
        $us = mysql_query($tes) or die (mysqli_error());
        $ux = mysql_fetch_assoc($us);
        echo $ux['id'];

    echo "<input type ='radio' name='x1' onchange='test2();' required id='max1' value ='".$ux['id']."'>";?>
     </td>

The code above will generate something like this (the number is id): 上面的代码将生成以下内容(数字为id):

在此处输入图片说明

I want to make like this: 我想这样:

在此处输入图片说明

but it become like this: 但它变成这样:

在此处输入图片说明

can someone tell me what wrong with my code ? 有人可以告诉我我的代码有什么问题吗?

You are using document.getElementById and trying to access multiple elements, that is not correct. 您正在使用document.getElementById并尝试访问多个元素,这是不正确的。 If you have multiple DOM elements to access and manipulate then you need to go with document.getElementsByClassName or document.getElementsBtTagName . 如果要访问和操作多个DOM元素,则需要使用document.getElementsByClassNamedocument.getElementsBtTagName In an HTML page, you can have only one element with a unique id (id of two elements can never be same). 在HTML页面中,只能有一个具有唯一ID的元素(两个元素的ID永远不能相同)。

In your case, you are using a loop and assigning the same id to all elements in the loop. 在您的情况下,您正在使用循环并将相同的ID分配给循环中的所有元素。 That is the problem. 那就是问题所在。

Thank you. 谢谢。

There is no need of JS logic, you have to use radio button grouping by name, if you are using loop then append same index in radio's name for both columns and then your html should be like, 不需要JS逻辑,您必须按名称使用单选按钮分组,如果使用循环,则在两列的单选名称中附加相同的索引,然后html应该像这样,

<tr>
    <td>
      ...
      <input type="radio" name="x_1" .../>
    </td>
    <td>
      ...
      <input type="radio" name="x_1" .../>
    </td>
</tr>
<tr>
    <td>
      ...
      <input type="radio" name="x_2" .../>
    </td>
    <td>
      ...
      <input type="radio" name="x_2" .../>
    </td>
</tr>

Read more about input-type-radio 进一步了解input-type-radio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM