通过javascript函数选择所选选项
我一直在尝试实施年提出的一个解决方案,但没有成功。要么向混合中添加select使解决方案无效,要么我犯了一些编译器和我都找不到的编码错误 我的html:通过javascript函数选择所选选项,javascript,jquery,dom,jquery-chosen,Javascript,Jquery,Dom,Jquery Chosen,我一直在尝试实施年提出的一个解决方案,但没有成功。要么向混合中添加select使解决方案无效,要么我犯了一些编译器和我都找不到的编码错误 我的html: <select name="superType" class="chosen-select chosen-select-creation-allowed" id="superType" style="width: 300px; display: none;" onchange="loadInheritance()" multiple=""
<select name="superType" class="chosen-select chosen-select-creation-allowed" id="superType" style="width: 300px; display: none;" onchange="loadInheritance()" multiple="">
<option style="padding-left:20px;" value=""></option><option style="padding-left:0px" value="28">concept</option>
<option style="padding-left:20px" value="30">human</option>
</select>
相反,根据DOM模型检查器和可视化表示,不会发生任何事情
我想要的是在函数中进行此选择,但当我尝试构建对上面选项的引用时,会出现错误:
Syntax error, unrecognized expression: '#superType option[value="30"]'
下面是生成该脚本的javascript(在“cid”上进行处理):
css选择器周围不需要单引号
$(`${target} option[value="${optionValue}"]`).attr('selected', 'selected');
为什么css选择器周围有单引号?
//now go back through and select the ones this CommonNoun already had from before
$.ajax({
url: '/BackToTech/server?function=getExistingSuperTypes',
type: 'POST',
success: function (response){
// var target = $("#superType");
var target = "#superType";
selectOptions(response, target, 0);
$('.chosen-select').trigger("chosen:updated");
}
});
function selectOptions(obj, target) {
var concepts = obj;
$.each(concepts, function(concept, value) {
alert(concept, value);
if (concept == "cid") {
optionValue = obj[concept].oid;
$("'" + target + " option[value=\"" + optionValue + "\"]'").attr('selected', 'selected');
} else {
selectOptions(value, target);
}
});
}
$(`${target} option[value="${optionValue}"]`).attr('selected', 'selected');