Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.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 将禁用的选定属性从变量放入选择框的匹配值中_Javascript_Jquery - Fatal编程技术网

Javascript 将禁用的选定属性从变量放入选择框的匹配值中

Javascript 将禁用的选定属性从变量放入选择框的匹配值中,javascript,jquery,Javascript,Jquery,我有这个选择框(参考下文) 现在您可以从上面看到,首先我有一个变量名valueofselectbox,然后,我循环遍历该选择框内的所有选项,然后从该选择框内的每个选项中删除禁用和选中的属性,然后添加禁用和选中的属性(如果其值为(选项值))等于变量,但似乎不起作用,相反,我在控制台中有一个错误“uncaughtreferenceerror:selectedisnotdefined” 您应该使用 $(this).removeAttr("disabled"); $(this).removeAttr("

我有这个选择框(参考下文)

现在您可以从上面看到,首先我有一个变量名valueofselectbox,然后,我循环遍历该选择框内的所有选项,然后从该选择框内的每个选项中删除禁用和选中的属性,然后添加禁用和选中的属性(如果其值为(选项值))等于变量,但似乎不起作用,相反,我在控制台中有一个错误“uncaughtreferenceerror:selectedisnotdefined”

您应该使用

$(this).removeAttr("disabled");
$(this).removeAttr("selected");
从jQuery文档:

设置属性时,每次使用1个属性名称,以便您可以使用:

$(this).attr("disabled", "");
$(this).attr("selected", true);

选定的
禁用的
是属性,通常用
prop()
修改,而不是
attr()
,什么是
$(这个)。attr(“禁用选定的”)
应该做什么?@adeneo:我应该在选项中添加两个属性,就像结果是添加两个属性一样,
$(这个)。attr({disabled:true,selected:true});
将布尔属性设置为
不会删除它。请使用
removeAttr
或将其设置为
false
$(this).removeAttr("disabled");
$(this).removeAttr("selected");
$(this).attr("disabled", "");
$(this).attr("selected", true);