如何检查单选按钮 [英] How to check radio buttons

查看:61
本文介绍了如何检查单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下MySQL表:

I have the following MySQL table:

|  id   |    Title    |  Windows | Linux | IDE | GUI | RAD |
------------------------------------------------------------
|   1   |  Software_1 |     1    |   0   |  1  |  0  |  0  |
------------------------------------------------------------
|   2   |  Software_2 |     0    |   1   |  0  |  1  |  0  |

我想通过查询mySQL数据库来填充表单.到目前为止,我能够填充输入字段,文本区域和复选框,但是我绝对会填充 radiobuttons .请注意,mySQL为以下复选框存储布尔值值:复选框(Windows,Linux) 和单选按钮(IDE,GUI,RAD)

I would like to populate a form by querying the mySQL db. Up to now I'm able to populate input fields, textareas and checkboxes but I'm absolutely stuck on populating radiobuttons. Please note that mySQL stores boolean values for: checkboxes (Windows, Linux) and radiobuttons (IDE, GUI, RAD)

这是PHP部分,可从dB中检索数据并将其转换为JSON:

This is the PHP part that retrieves the data from the dB and converts it to JSON:

while($review = mysql_fetch_array($qry)) {
    $arr = array('id_field' => $review['id'],
                'title' => $review['title'],
                'homepage' => $review['link_homepage'],
                'windows' => $review['Windows'] == "1",
                'linux' => $review['Linux'] == "1",
                'ide' => $review['IDE'] == "1",
                'gui' => $review['GUI'] == "1",
                'rad' => $review['RAD'] == "1"
    );
    echo json_encode($arr);
} 

这是表格:

<form name="form" id="edit_review" method ="post" action="review.php">  
    Language name: <input type="text" size="34" value="" name="title"/>
    Windows <input type="checkbox" name="windows" />
    Linux <input type="checkbox" name="linux" />

    IDE <input type="radio" name="environment" id="ide" value="ide"/>
    GUI <input type="radio" name="environment" id="gui" value="gui"/>
    RAD <input type="radio" name="environment" id="rad" value="rad"/>
</form>

这是jQuery代码:

This is the jQuery code:

$(".editlink").click(function() {
    $.get("lists.php", {param2: $(this).attr('id')}, 
        function(data) {
            jsonOBJ = jQuery.parseJSON(data);
            for (var key in jsonOBJ) {
                $(":input[name='" + key + "']").val(jsonOBJ[key]);
                $(":input[name='" + key + "']").attr("checked", jsonOBJ[key]);
            }
        }   
    );    
    return false;
 });

字符串:

$(":input[name='" + key + "']").attr("checked", jsonOBJ[key]);

仅适用于复选框.单选按钮的名称"是环境",并且对于所有三个单选按钮必须相同.因此,上面的线当然是行不通的.我尝试将其更改为:

works only for checkboxes. Radiobuttons 'name' is 'environment' and must be the same for all the three radiobuttons. So, of course the above line is not working. I tried changing it to:

$(":input[id='" + key + "']").attr("checked", jsonOBJ[key]);

,我在单选按钮中添加了" id ",如下所示:

and I added 'id' to the radiobuttons as follows:

IDE <input type="radio" name="environment" id="ide" value="ide"/>

我注意到(使用警告")选中了正确的复选框,但是一旦LOOP继续运行,便立即取消了选中.为什么未选中单选按钮而未选中复选框?我该如何解决?

and I noticed (using 'alert') that the correct checkbox is checked but as soon as the LOOP continues is immediately unchecked. Why radiobuttons are unchecked and checkboxes are not? How can I fix it?

推荐答案

这取决于您使用的jquery的版本.请查看:如何在jQuery中重置单选按钮,以便不进行任何检查您的答案.

It depends on the version of jquery you are using. Please look at: How to reset radiobuttons in jQuery so that none is checked for your answer.

这篇关于如何检查单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆