我需要你的帮助,
如何查看是否选择框选项值包含匹配的' 08C'并警告(如果确实)。
工作的一个例子或答案将不胜感激。提前抱歉缺乏编码技能,因为这超出了我的想法。
使用jQuery的答案也是可以接受的。
<select id="fin-code-lobj">
<optgroup label="08C - Travel for Training">
<option value="08C1">08C1 - Travel for Technical/Operational Training – Mandatory (for/by TC employees)</option>
<option value="08C2">08C2 - Travel for Technical/Operational Training – Non-Mandatory</option>
<option value="08C3">08C3 - Travel for Developmental Training</option>
<option value="08C4">08C4 - Travel for Developmental Language Training</option>
<option value="08C5">08C5 - Travel for Statutory Language Training</option>
</optgroup>
</select>
答案 0 :(得分:1)
您可以使用options
在jquery中获取$("#fin-code-lobj option")
,然后像这样检查
$("#fin-code-lobj option").each(function() {
$(this).val().indexOf("08C") != -1 ? console.log($(this).val() + "- true"): console.log($(this).val() + "- false");
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="fin-code-lobj">
<optgroup label="08C - Travel for Training">
<option value="08C1">08C1 - Travel for Technical/Operational Training – Mandatory (for/by TC employees)</option>
<option value="08C2">08C2 - Travel for Technical/Operational Training – Non-Mandatory</option>
<option value="08C3">08C3 - Travel for Developmental Training</option>
<option value="08C4">08C4 - Travel for Developmental Language Training</option>
<option value="08C5">08C5 - Travel for Statutory Language Training</option>
<option value="11">Dummy</option>
</optgroup>
</select>
&#13;
答案 1 :(得分:1)
这应该可以解决问题:
$("#fin-code-lobj").on("change", function(){
if($(this).find("option:selected").val().indexOf("08C") > -1)){
alert(true);
}
});
答案 2 :(得分:0)
如果出现以下警告:
08c
开头(不区分大小写)
const matchOptionVal = ( options, str ) => alert (
[...options].some(op => new RegExp("^"+str, "i").test(str) )
);
[...document.querySelectorAll("select")].forEach(sel =>
matchOptionVal(sel.options, "08c")
);
&#13;
<select id="fin-code-lobj">
<optgroup label="08C - Travel for Training">
<option value="08C1">08C1 - Travel for Technical/Operational Training – Mandatory (for/by TC employees)</option>
<option value="08C2">08C2 - Travel for Technical/Operational Training – Non-Mandatory</option>
<option value="08C3">08C3 - Travel for Developmental Training</option>
<option value="08C4">08C4 - Travel for Developmental Language Training</option>
<option value="08C5">08C5 - Travel for Statutory Language Training</option>
</optgroup>
</select>
&#13;
答案 3 :(得分:0)
<强>答案强> 这是一个解决方案,它将遍历每个值,试图找到你想要的字符串。用纯JS编写,这样就没有库或框架依赖。
function funStart(){
var objDropDown = document.querySelector("#fin-code-lobj");
if (typeof(objDropDown)=="undefined"){
console.log("Dropdown not located");
return;
}
for (a = 1; a <= objDropDown.length; a++){
if (objDropDown[a-1].value == "08C3"){
alert("I found 08C5");
console.log ("Found and alerted" + a);
}
}
}
document.addEventListener("DOMContentLoaded", funStart, false);
&#13;
<select id="fin-code-lobj">
<optgroup label="08C - Travel for Training">
<option value="08C1">08C1 - Travel for Technical/Operational Training – Mandatory (for/by TC employees)</option>
<option value="08C2">08C2 - Travel for Technical/Operational Training – Non-Mandatory</option>
<option value="08C3">08C3 - Travel for Developmental Training</option>
<option value="08C4">08C4 - Travel for Developmental Language Training</option>
<option value="08C5">08C5 - Travel for Statutory Language Training</option>
</optgroup>
</select>
&#13;