Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 难以将变量插入jquery属性标识_Javascript_Jquery_Checkbox - Fatal编程技术网

Javascript 难以将变量插入jquery属性标识

Javascript 难以将变量插入jquery属性标识,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我试图使用jquery来检测是否选中了同一组中的另一个文本框。下面的代码显示了我如何在选中“高级”框时检索组名,并使用它查看是否选中了附带的“基本”框。问题是,无论基本复选框的条件如何,“basicTrue”总是被指定为“undefined” <div id="boxes"> <input style="text-align:center;" type="checkbox" name="group1" value="Basic"> <input style="tex

我试图使用jquery来检测是否选中了同一组中的另一个文本框。下面的代码显示了我如何在选中“高级”框时检索组名,并使用它查看是否选中了附带的“基本”框。问题是,无论基本复选框的条件如何,“basicTrue”总是被指定为“undefined”

<div id="boxes">
<input style="text-align:center;" type="checkbox" name="group1" value="Basic">
<input style="text-align:center;" type="checkbox" name="group1" value="Advanced">
<input style="text-align:center;" type="checkbox" name="group2" value="Basic">
<input style="text-align:center;" type="checkbox" name="group2" value="Advanced">
</div>

$("#boxes").contents().find(":checkbox").bind('change', function(){        
val = this.checked;
var $obj = $(this);    
if($obj.val()=="Advanced"){
var group = $obj.attr("name");
var basicTrue = $('input[name=group][value="Basic"]').prop("checked");
if(basicTrue)
{
    //Do stuff
}
else
{
    $obj.attr('checked', false);
}
}

我知道变量“group”的名称是正确的:例如group1。在代码中使用此变量有什么原因不起作用吗?

这些是变量,需要在选择器的字符串中包含它们,如下所示:

$('input[name="' + group + '"][value="Basic"]').prop("checked");
简化版:

$("#boxes input[type='checkbox']").on('change', function(){        
   var bT = $('input[name="'+ this.name +'"][value="Basic"]').prop("checked");

    if( this.value == "Advanced" && bT) {
        //Do stuff
    } else {
        $(this).prop('checked', false);
    }
});

谢谢,在写了一个变量不属于字符串的问题后,我发现了这个问题。不过,不需要为它添加引号。
$("#boxes input[type='checkbox']").on('change', function(){        
   var bT = $('input[name="'+ this.name +'"][value="Basic"]').prop("checked");

    if( this.value == "Advanced" && bT) {
        //Do stuff
    } else {
        $(this).prop('checked', false);
    }
});