我正在尝试从下拉列表中重新分配选定的值,但我不确定如何执行此操作。我尝试了几件事,但没有运气......
1)that.value =“Aqua”;
2)$(that).val(“Aqua”);
3)$(that).attr(“value”,“Aqua”);
任何帮助都将不胜感激。
<select onchange="yesnoCheck(this);" name="name" id="selectedColor" required >
<option value="" selected>Select an Option</option>
<option value="Blue">Option 1</option>
<option value="Orange">Option 2</option>
<option value="Red">Option 3</option>
</select>
<script>
function yesnoCheck(that) {
if (that.value == "Blue" ) {
that.value = "Aqua"; /* <----- re-assign the value */
/* commented out */
/* $(that).val("Aqua");
$(that).attr("value", "Aqua"); */
}
...
}
答案 0 :(得分:0)
你的问题有点令人困惑
您的意思是在选择后更改文字吗?您可以执行以下代码
</script>
<select onchange="yesnoCheck(this);" name="name" id="selectedColor" required >
<option value="" selected>Select an Option</option>
<option id="change" value="Blue">Option 1</option>
<option value="Orange">Option 2</option>
<option value="Red">Option 3</option>
</select>
<script>
function yesnoCheck(e) {
if (e.value == "Blue" ) {
$("#change").text("Aqua");
}
}
</script>
或者,您想要将选项值从“Blue”更改为“Aqua”,然后
$("#change").val("Aqua");