当存储的值="on"时,如何在启动时将html单选按钮显示为选中状态? [英] How do I make an html radio button display as selected at launch time when the stored value="on"?

查看:70
本文介绍了当存储的值="on"时,如何在启动时将html单选按钮显示为选中状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JavaScript和html:

Using Javascript and html:

我有一个包含单选按钮的列表,该列表在启动时根据存储的数据动态加载.即使无线电的存储值处于开"状态,启动时无线电也不会显示为选中状态.

I have a list containing radio buttons that is dynamically loaded at launch time based on stored data. Even though the stored value of the radio is "on" the radio at launch does not display as selected.

我找不到我想要的东西.

I cant figure out what I'm missing.

if (defsales !== undefined) {
    $('.defsales', newRow).val(defsales); }

defsales是收音机的类别标识符.

defsales being the class identifier of the radio.

是否需要其他参数来检查值? 或者,我是否需要添加一些在启动后重新应用该值的东西?

Do I need an additional argument to check the value? Or, do I need to add something that reapplies the value after launch?

我已经在这个问题上工作了很长时间,以至于我感到困惑.

I've been working at this problem long enough that I'm getting confused.

请原谅我的无知,

如果有帮助,下面是该功能的其余部分:

If it helps, here is the rest of the function:

function addSalespersonRow(id, name, phone, email, defsales) {
var newRow = $('<tr>'+
    '<td><input type="hidden" class="id" /><input class="name" size="20" /></td>'+
    '<td><input class="phone" size="15"/></td>'+
    '<td><input class="email" size="30"/></td>'+
    '<td><input type="radio" name="salespersons" class="defsales" /></td>'+
    '<td><a href="#" onclick="$(this).parent().parent().remove(); return false;">Delete</a></td>'+
    '</tr>');
if (id === undefined) {
    id = new Date().getTime();
}
$('.id', newRow).val(id);


if (name !== undefined) {
    $('.name', newRow).val(name);
}
if (phone !== undefined) {
    $('.phone', newRow).val(phone);
}
if (email !== undefined) {
    $('.email', newRow).val(email);
}
if (defsales !== undefined) {
    $('.defsales', newRow).val(defsales);
}
$('#salespersons-table tbody').append(newRow);

return false;
}

这是正确运行的代码.

if (defsales !== undefined && defsales == "on") {
    $('.defsales', newRow).attr('checked', true);
}

感谢马特(Matt)向我指出正确的方向.

Thanks to Matt, for pointing me in the right direction.

推荐答案

使用JQuery,您应该可以检查任何这样的单选按钮.

Using JQuery you should be able to check any radio button like this.

$(".dfsales").attr("checked", true); 

这似乎可行:

$(document).ready(function(){
    var dfsales = 1;
    $('<input type="radio" name="colorInput" id="test" value="2">').appendTo($("#list"));
    if(dfsales !== undefined){
        $('#test').attr('checked', true);
    }
})

埃里克(Eric),假设您仅在设置dfsales ==起作用的情况下才设置单选按钮的值.

EDIT 2: Eric, assuming you only care to set the value of the radio button if dfsales == on this will work.

if (defsales !== undefined && defsales == 'on') {
    $('.defsales', newRow).val(defsales).attr('checked', true);
}

如果您希望无论如何都设置单选按钮值,请尝试执行以下操作:

if you want the radio button value to be set regardless, then try something like this:

if (defsales !== undefined){
    $('.defsales', newRow).val(defsales);
    if(defsales == 'on'){
        $('.defsales', newRow).attr('checked', true);
    }
}

有帮助吗?

这篇关于当存储的值="on"时,如何在启动时将html单选按钮显示为选中状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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