gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:24:49 30 4
gpt4 key购买 nike

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

HTML:

 <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>

JavaScript:

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;

有什么建议吗? :)

非常感谢!

最佳答案

您似乎没有 id 为 ContentPlaceHolder1_personDetails_titleDropDownList 的元素,因此无法使用 document.getElementById 获取该元素,因此您很可能会收到错误未捕获类型错误:无法在控制台中读取 null 的属性“selectedIndex”

编辑:您是否打算使用 s2id_ContentPlaceHolder1_personDetails_genderDropDownList 作为选择器?

关于Javascript 自动选择下拉列表而不选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45155061/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com