Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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_Html_Select_Option - Fatal编程技术网

Javascript自动选择下拉列表,不选择

Javascript自动选择下拉列表,不选择,javascript,html,select,option,Javascript,Html,Select,Option,我正在为一个网站编程,我希望自动选择所有下拉列表,而不是手动选择。 问题是,我的JS代码带来了值,但它没有选择任何选项,因此该框保持“选择一个选项”的状态 HTML: 有什么建议吗?:) 非常感谢 您似乎没有id为contentplaceholder 1\u personDetails\u titleDropDownList的元素,因此无法使用document.getElementById获取元素,因此您很可能会收到错误未捕获类型错误:无法读取控制台中null的属性'selectedIndex'

我正在为一个网站编程,我希望自动选择所有下拉列表,而不是手动选择。 问题是,我的JS代码带来了值,但它没有选择任何选项,因此该框保持“选择一个选项”的状态

HTML:

有什么建议吗?:)


非常感谢

您似乎没有id为
contentplaceholder 1\u personDetails\u titleDropDownList
的元素,因此无法使用
document.getElementById
获取元素,因此您很可能会收到错误
未捕获类型错误:无法读取控制台中null
的属性'selectedIndex'


Edit:您是否打算使用
s2id\u ContentPlaceHolder1\u personDetails\u genderDropDownList
作为选择器?

您似乎没有id为
ContentPlaceHolder1\u personDetails\u titleDropDownList
的元素,因此无法使用
document.getElementById
获取元素最有可能出现错误
uncaughttypeerror:无法在控制台中读取null的属性'selectedIndex'


编辑:您是否打算使用
s2id\u ContentPlaceHolder1\u personDetails\u genderDropDownList
作为您的选择器?

非常感谢桑蒂莫!!是的,我为此道歉。我指的是性别,不是头衔。对不起,我很累:(不客气!如果这解决了你的问题,你可以接受我的回答,然后人们就会知道这已经解决了:)我很乐意!但问题依然存在。当我在浏览器控制台中编写JS代码时,下拉菜单不会改变。他们保留“选择一个选项”。我想我需要自动点击来自动选择我的下拉列表,但我不知道怎么做,我以前尝试过的一切都不起作用我不太确定你想要实现什么,但我刚刚写了一篇关于使用javascript选择选项的文章——也许这会有所帮助?非常感谢Suantimo!!是的,我为此道歉。我指的是性别,不是头衔。对不起,我很累:(不客气!如果这解决了你的问题,你可以接受我的回答,然后人们就会知道这已经解决了:)我很乐意!但问题依然存在。当我在浏览器控制台中编写JS代码时,下拉菜单不会改变。他们保留“选择一个选项”。我想我需要自动点击以自动选择下拉列表,但是我不知道怎么做,我以前尝试过的所有方法都不起作用我不太确定你想要实现什么,但我只是写了一篇关于使用javascript选择一个选项的文章——也许这会有所帮助?你的
s2id\u contentplaceholder\u personDetails\u genderDropDownList
(长得惊人的id btw)是一个
div
,不是一个
select
,所以在上面调用
.value
.options
等不会导致任何错误,如果您幸运的话,谢谢!但我尝试使用select属性,但没有成功。简单地将其更改为
div
仍然无法神奇地修复它。如果你整理一下你的代码,你会发现你的JS中有错误。你漏掉了一个引号,
s2id\u autogen1
从来没有引用过DOM元素,没有进一步检查你的
s2id\u ContentPlaceHolder1\u personDetails\u genderDropDownList
(长得惊人的id btw)是一个
div
,而不是
select
,因此在上面调用
.value
选项等,如果你运气好的话,除了一个错误什么都不会发生谢谢!但我尝试使用select属性,但没有成功。简单地将其更改为
div
仍然无法神奇地修复它。如果你整理一下你的代码,你会发现你的JS中有错误。您遗漏了一个引号,
s2id\u autogen1
从未引用过DOM元素,没有进一步检查
 <tr> <td>Gender</td> <td> <div class="select2-container select2-container-active" id="s2id_ContentPlaceHolder1_personDetails_genderDropDownList"> <a href="javascript:void(0)" class="select2-choice select2-default" tabindex="-1"> <span class="select2-chosen" id="select2-chosen-2">Select an Option</span> <abbr class="select2-search-choice-close"></abbr> <span class="select2-arrow" role="presentation"> <b role="presentation"></b></span></a><label for="s2id_autogen2" class="select2-offscreen"></label> <input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-2" id="s2id_autogen2" tabindex="0" autocomplete="Female"></div> <select name="_ctl0:ContentPlaceHolder1:personDetails:genderDropDownList" id="ContentPlaceHolder1_personDetails_genderDropDownList" onchange="nzis.UpdateDropdownErrorLabel(this)" errorlabel="mandatory" tabindex="-1" title="" style="display: none;"> <option selected="selected" value=""></option> <option value="M">Male</option> <option value="F">Female</option> </select> </td> </tr>
var index = document.getElementById('s2id_ContentPlaceHolder1_personDetails_genderDropDownList).selectedIndex; alert("value="+document.getElementById('s2id_ContentPlaceHolder1_personDetails_genderDropDownList').value); alert("text="+document.getElementById('s2id_ContentPlaceHolder1_personDetails_genderDropDownList').options[1].text); s2id_autogen1.value= document.getElementById('s2id_ContentPlaceHolder1_personDetails_genderDropDownList').options[2].text; s2id_autogen1.checked = true; var index = document.getElementById('s2id_ContentPlaceHolder1_personDetails_genderDropDownList').selectedIndex; alert("value="+document.getElementById('s2id_ContentPlaceHolder1_personDetails_genderDropDownList').value); alert("text="+document.getElementById('s2id_ContentPlaceHolder1_personDetails_genderDropDownList').options[1].text); s2id_autogen2.value= document.getElementById('s2id_ContentPlaceHolder1_personDetails_genderDropDownList').options[2].text;