简体   繁体   English

javascript单击下拉列表中的值

[英]javascript click a value in dropdown list

I have the following code written in HTML: 我有以下用HTML编写的代码:

    <table cellspacing="0" cellpadding="0" width="100%" class="HeaderTable">
    <tbody><tr>
        <td id="dnn_ctr6707_TimeTableView_TdClassesList" class="HeaderClassesCell">
            class
            <select name="dnn$ctr6707$TimeTableView$ClassesList" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;dnn$ctr6707$TimeTableView$ClassesList\&#39;,\&#39;\&#39;)&#39;, 0)" id="dnn_ctr6707_TimeTableView_ClassesList" class="HeaderClasses">
            <option selected="selected" value="14">a</option>
            <option value="15">b</option>
            <option value="16">c</option>
            <option value="17">d</option>
            <option value="49">e</option>
            <option value="60">f</option>
        </select></td>

What i'm trying to do, is programmly click the option 'b' In the console tab. 我正在尝试做的是,在控制台选项卡中单击选项'b'。 for some reason it doesn't work for me altho it seems good I think. 出于某种原因,它对我来说不起作用,但我觉得这似乎很好。

Here's what I tried: 这是我试过的:

var x = document.getElementById('dnn_ctr6707_TimeTableView_ClassesList') // this part work
var y = x.options[1].click() // I managed to get the text of the option but I want it to perform click and it doesn't work that way :(

Thank you for your help! 谢谢您的帮助!

I think what you want to do is set the value of the select instead of clicking on the option. 我想你想要做的是设置select的值而不是单击选项。

Try this instead: 试试这个:

document.getElementById('dnn_ctr6707_TimeTableView_ClassesList').value = "15"

I think what your asking is for an event to be activated. 我想你要求激活一个事件。 Javascript doesnt have a .click() function i believe. 我相信Javascript没有.click()函数。

var x = document.getElementById('dnn_ctr6707_TimeTableView_ClassesList');
var evt = new Event('click');
x.options[1].dispatchEvent(evt);

Here is some simple code that lets you enter an index into a text field and the pick that index from the <select> element. 下面是一些简单的代码,可以让您在文本字段中输入索引,并从<select>元素中<select>索引。

 var btn = document.querySelector('#select'); var inp = document.querySelector('#inp'); var sel = document.querySelector('#dnn_ctr6707_TimeTableView_ClassesList'); btn.addEventListener('click', function() { var val = parseInt(inp.value, 10); sel.selectedIndex = val; }); 
 class <select name="dnn$ctr6707$TimeTableView$ClassesList" id="dnn_ctr6707_TimeTableView_ClassesList" class="HeaderClasses"> <option selected="selected" value="14">a</option> <option value="15">b</option> <option value="16">c</option> <option value="17">d</option> <option value="49">e</option> <option value="60">f</option> </select> <hr/> <input id="inp" type="text" value="0"/> <button id="select">Select</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM