繁体   English   中英

标记单选按钮默认为选中状态

[英]Marking Radio Button checked by default

我有一个表格,我试图自动填充单选按钮的值。

content=content+' <label class="radio-inline">';                
content=content+' <input type="radio" name="ip_radio_resp_'+questDetail.qId+'" id="radioYes"  value="Y" onclick="removeFocusOnComment('+questDetail.qId+');showReasonTextBox('+questDetail.qId+')"> Yes';
content=content+' </label>

我正在检查其他字段的值,如果我得到所需的响应,则将其标记为btn,否则不会

switch(questDetail.metricsValue){
case 'Y':

    $('#ip_radio_resp_'+questDetail.qId).each(function() {
        $('input:radio[name="ip_radio_resp_'+questDetail.qId+'"]#radioYes').prop('checked',true);
    });
    break;
case 'N' :
    //something......   
break;
default:
    //something
}

我正在使用jQuery 1.11.2

但是它不断地给我未定义的错误..我知道这可能是重复的,我尝试了给出的所有其他答案,但无法正常工作...请有人帮忙!

提前致谢。

您不能在每个广播组中都有id="radioYes" ,因为ID必须是唯一的。 请改用class="radioYes" 然后,您可以执行以下操作:

case 'Y':
    $('input:radio[name="ip_radio_resp_'+questDetail.qId+'"].radioYes').prop('checked',true);
    break;

您不需要使用$('#ip_radio_resp_'+questDetail.qId).each是因为您没有对具有该ID的元素进行任何操作(我怀疑您甚至没有该元素,而且您对name感到困惑id )。

我认为您并不需要所有这些,您只需要在使单选按钮时检查一下是否

questDetail.metricsValue == 'Y'

那么您需要对其进行检查。

您可以取消创建的所有切换大小写,并以这种方式进行操作,这对您来说会更加容易。

    content=content+' <label class="radio-inline">';                
    content=content+' <input type="radio" name="ip_radio_resp_'+questDetail.qId+'" class="radiosToBeChecked" id="radioYes'+questDetail.qId+'" checked="'+(questDetail.metricsValue == 'Y') ? 'checked': ''+'" onclick="removeFocusOnComment('+questDetail.qId+');showReasonTextBox('+questDetail.qId+')"> Yes';
    content=content+' </label>';

暂无
暂无

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

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