如何设置选择选项可以按数字键入

时间:2018-03-03 05:13:36

标签: javascript html

我想在数字中设置我的选择选项。这是我的代码。



 <select style="width:100px;" autofocus>
 <option disabled selected value></option>
  <option value="6">6</option>
   <option value="5">5</option>
   <option value="4">4</option>
   <option value="3"></option>
   <option value="2">2</option>
   <option value="1">1</option>
  </select>
&#13;
&#13;
&#13;

如果我键入数字6,则选择选项应显示6而不选择选项值。请帮帮我。

1 个答案:

答案 0 :(得分:0)

假设您在文本框中键入数字6,其ID为#34; inputarea&#34;。

现在您可以从该输入框中获取键入的值,如下所示;

var inputval = docuemnt.getElementById("inputarea").value;

现在可以通过; {/ p>将inputval设置为选择框

document.getElementsByTagName("select")[0].value = inputval;

请注意,[0]表示页面中的第一个选择框,如果您有多个此类框,则必须相应地放置索引

现在让我们想要你想要的东西就像没有输入框一样,只要你按下按钮6就可以选择选择框。

然后试试这个;

 window.onload = function(){
 {
    document.getElementsByTagName("select")[0].focus();  // this will focus your select box
 }

现在尝试按下按钮6.