Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过选择列表中的文本而不是通过索引进行选择_Javascript - Fatal编程技术网

Javascript 通过选择列表中的文本而不是通过索引进行选择

Javascript 通过选择列表中的文本而不是通过索引进行选择,javascript,Javascript,我是JavaScript新手 我知道如何通过 document.getElementById("entry_1000001").selectedIndex=2 但是有没有什么方法可以直接通过文本进行选择 document.getElementById("entry_1000001").select("Ruby") HTML元素如下 <select name="entry.1000001" id="entry_1000001" aria-label="What language does

我是JavaScript新手

我知道如何通过

document.getElementById("entry_1000001").selectedIndex=2
但是有没有什么方法可以直接通过文本进行选择

document.getElementById("entry_1000001").select("Ruby")
HTML元素如下

<select name="entry.1000001" id="entry_1000001" aria-label="What language does Watir-WebDriver use?  " aria-required="true" required=""><option value=""></option>
<option value="Java">Java</option> <option value="Ruby">Ruby</option> <option value="C#">C#</option> <option value="Clojure">Clojure</option> <option value="Python">Python</option></select>

Java Ruby C#Clojure Python
document.getElementById(“条目_1000001”).value=“Ruby”

JAVA
红宝石
C#
Clojure
python

当然,
document.getElementById(“entry_1000001”).value=“Ruby”我以为这个值方法是用于文本字段的,我不知道选择列表也有这个值方法,谢谢。@RAJ是的,(如果该选项存在),它会在引擎盖下将其设置为
选中的
。您可以稍后通过执行
var sel=document.getElementById(“entry_1000001”)轻松测试上述内容;日志(选择选项[selectedIndex])document.getElementsByName(“foo”)[0]
其中
0
是元素的索引,或者通过循环检索到的集合来定位元素。更新的、稍微慢一点的(根本不重要)版本是
document.querySelector(“[name='foo']”)
非常棒,谢谢!我现在开始学习JavaScript,以便嵌入到我的自动化脚本中。非常感谢。