Javascript Can';t在单选按钮中设置选中属性(表内单选按钮)

Javascript Can';t在单选按钮中设置选中属性(表内单选按钮),javascript,php,jquery,html,mysql,Javascript,Php,Jquery,Html,Mysql,我尝试将单选按钮checked设置为checked=“checked”,但这似乎不起作用。 我试过使用 $(id_radio).prop('checked',true) document.getElementById(“id_radio”).checked=true document.getElementById(“id_radio”).checked=true 文档.getElementById(“id_radio”)。已选中 $(id\u radio).attr('checked',chec

我尝试将单选按钮checked设置为
checked=“checked”
,但这似乎不起作用。 我试过使用

  • $(id_radio).prop('checked',true)
  • document.getElementById(“id_radio”).checked=true
  • document.getElementById(“id_radio”).checked=true
  • 文档.getElementById(“id_radio”)。已选中
  • $(id\u radio).attr('checked',checked) 但它们似乎都没有将
    checked
    的属性更改为
    checked=“checked”

    下面是我的代码 js_file.js

    $(document).ready(function(){
    $("document").on("click",".class_radio3",function(){
    
      var id_radio=$(this).attr('value');
      $(id_radio).prop('checked',true);
      console.log(id_radio);
    });
    });
    
    创建表的代码

    单选按钮变得不可点击。为什么会这样

    更新1:问题所在的图片


    我认为这里有几个问题。。。一是每组只能检查一台收音机,即具有相同“名称”的收音机。选中
    checked=“”
    checked=“checked”
    相同-缺少该属性即为“未选中”状态

    div{
    宽度:50%;
    浮动:左;
    }
    
    第一组
    一,

    二,

    三,

    四,

    五,

    第2组 一,

    二,

    三,

    四,

    五,

    试试这个 HTML

    检查下面的

    您可以像下面一样使用attr,但我建议您使用

    函数chk(元素){
    $(元素).attr(“选中”、“选中”);
    }
    
    10
    15
    
    这是打印还是不打印??控制台日志(def_收音机)@HEMANTSUMAN打字错误,我刚刚纠正了它,但它确实打印了正确的#id选择请包括直接的HTML-客户端看到的内容(PHP仅限于服务器端)。您可以通过页面源代码看到这一点。@allicarn我已经包括了客户机将看到的表。服务器端mysql连接可以正常工作,因为其他数据可以输入和删除。为什么您可以获得单选按钮的值,只需执行$(this).prop('checked',true);
    name
    class
    是一回事吗?正如我假设的那样,这就像将它们分组在同一个categoryno下一样,类用于样式,也可能在JS中用作选择器。名称是表单元素的名称,在提交表单时,它将成为提交对象中的“键”。然而,我想我误解了你的问题。将在一秒钟内更新。@peekaboo请查看我的更新。希望这会有所帮助。嗯,我明白了,但是当我单击其他单选按钮时,您的代码似乎会选中新的单选按钮,但是之前的条目仍然保持为选中状态,然后使用id=“radio”.$row\u msg\u id和$('radio\u'+id\u radio).prop('checked',true);请解决语法错误刚刚尝试了你的方法,不起作用,你犯了一个语法错误,应该是'id='radio'$row'\u msg''id'
        <br>
        <p class="uk-text-primary uk-text-bold"><u> General Message </u></p>
        <table class="uk-table uk-border-rounded uk-grid-width-* uk-panel-box uk-text-justify" border="1">
            <!--create top table -->
            <tr> <!--tr defines a row --> <!-- define row 1 -->
                <th class="uk-width-1-10"> Recipient </th> <!-- th defines a header cell--> 
                <th class="uk-width-1-3"> Message </th>
                <th class="uk-width-1-10"> Action </th>
                <th class="uk-width-1-10"> Default message </th>
            </tr>
             <!-- row 2 : display results onwards and creates dynamically -->   
            <!-- input fields at the bottom -->
    
    
                <?php
                    include "../connect.php"; 
                    $get_data2admin = $db->query("SELECT ID,message,user FROM test_table_1 WHERE user='General'");
    
    
                if( $get_data2admin->num_rows >0 ){ //if selected rows is more than zero
                    while($row_msg = $get_data2admin->fetch_assoc()){
    
                        $row_msg_id=$row_msg['ID']; //$row_msg['ID'] directly using it wont display in id tag attribute
                        echo "<tr>";
                        echo "<td>".$row_msg['user']."</td>";
                        echo "<td>".$row_msg['message']. "</td>";
    
                        ?>
    
                        <td><a href="#" id="<?php echo $row_msg_id; ?>" class="delete_button uk-text-danger">Delete</a></td>
    
                        <?php 
                        if($row_msg['user']=='General'){
                            echo "<td>"."<input class='class_radio3' type='radio' name='name_radio2' value='$row_msg_id' checked=''/>"."</td>";
    
                        }
                        else{
                            echo "<td>"."-"."</td>"; 
                        };
    
                        echo "</tr>";
                    }
                }
    
            ?>
    </table>
    
    $("document").on("click",".class_radio3",function(e){
    e.preventDefault();
    
      if($row_msg['user']=='General'){
     echo "<td>"."<input id='$row_msg_id' class='class_radio3' type='radio' name='name_radio2' value='$row_msg_id' />"."</td>";
    
                            }
    
    var id_radio=$(this).attr('id');
      $('#' + id_radio).prop('checked',true);