Javascript 按值选择 2 四 6

Javascript 按值选择 2 四 6,javascript,Javascript,我有值4,现在如何使用javascript使用值4选择selectBox <select id="selectBox"> <option value="2"> two </option> <option value="4"> four</option> <option value="6"> six </option> </select> 你也可以这样做 document.getEl

我有值4,现在如何使用javascript使用值4选择
selectBox

<select id="selectBox">
  <option value="2"> two </option>
  <option value="4"> four</option>
  <option value="6"> six </option>
</select>



你也可以这样做

document.getElementById("selectBox").value=4;​
4

默认情况下,它会被选中vale 4

您也可以这样做

document.getElementById("selectBox").value=4;​
4

默认情况下,它会被选中vale 4

您在找这个吗

 <option value="4" selected="selected"> four</option>

功能fnc(myid、other1、other2)
{
document.getElementById(myid).text=myid+“被选中”;
document.getElementById(other1.text=other1;
document.getElementById(other2.text=other2;
}
2
四
6

你在找这个吗

 <option value="4" selected="selected"> four</option>

功能fnc(myid、other1、other2)
{
document.getElementById(myid).text=myid+“被选中”;
document.getElementById(other1.text=other1;
document.getElementById(other2.text=other2;
}
2
四
6

如果您使用的是Jquery,则可以在javascript运行时进行编码。首先,您必须选择
four
即:
$(“#selectBox option[value='4'])
然后您必须将selected更改为true。代码如下:


$(“#选择框选项[value='4']”)attr('selected',true)

如果您使用的是Jquery,那么可以在javascript运行时进行编码。首先,您必须选择
four
即:
$(“#selectBox option[value='4'])
然后您必须将selected更改为true。代码如下:


$(“#选择框选项[value='4']”)attr('selected',true)

我不确定。您想用4选择选项值还是同时更改选项文本?因为如果你只想被选中,那么它非常简单,答案如下。我正在尝试将文本更改为选中的
,但现在@curt解决方案也适用于我,我不确定。您想用4选择选项值还是同时更改选项文本?因为如果你只想被选中,那么它非常简单,答案如下。我正在尝试将文本更改为
selected
,但现在@curt解决方案也适用于我,不像这样。就像这个输出
four
。我在前面的评论上犯了错误。我应该写选项属性而不是选项文本。不是那样的。就像这个输出
four
。我在前面的评论上犯了错误。我应该写选项属性而不是选项文本。