我想把数字90作为默认值。有人知道如何将selected元素(如下面的HTML )应用于jQuery吗?
HTML中的示例
<select id="threshold">
<option value="90" selected>90</option> /* example selected in HTML */
</select>如何在selected中应用jQuery中的数字90作为默认值?
$("#threshold").append($("<option>",{value: "70",text: "70%"}));
$("#threshold").append($("<option>",{value: "80",text: "80%"}));
$("#threshold").append($("<option>",{value: "90",text: "90%"}));发布于 2019-11-08 12:43:39
任一
$("#threshold").append($("<option>",{ value: "90",text: "90%", selected:true }));
$("#threshold")
.append($("<option>",{value: "70",text: "70%"}))
.append($("<option>",{value: "80",text: "80%"}))
.append($("<option>",{value: "90",text: "90%", selected:true }))<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="threshold">
</select>
或
$("#threshold")
.append($("<option>",{value: "90",text: "90%"}))
.val("90");
$("#threshold")
.append($("<option>",{value: "70",text: "70%"}))
.append($("<option>",{value: "80",text: "80%"}))
.append($("<option>",{value: "90",text: "90%"}))
.val(90);<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="threshold">
</select>
较短:
const curTH = 90;
$.each([70, 80, 90], (_, item) =>
$("<option>",{ value: item, text: item + "%", "selected": item === curTH ? true : false })
.appendTo("#threshold")
)<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="threshold">
</select>
https://stackoverflow.com/questions/58766718
复制相似问题