简体   繁体   English

根据下拉选择填充2个输入

[英]Filling 2 inputs based on dropdown selection

I am looking to fill 2 fields based on the selection of a dropdown. 我希望根据下拉列表的选择填充2个字段。 This is the code I am using now (which works but not work in my case): 这是我现在正在使用的代码(可以使用,但在我的情况下不起作用):

<script type="text/javascript">
function updateinput(){
var e = document.getElementById("event");
var catSelected = e.options[e.selectedIndex].value;

document.getElementById("minLvl").value=catSelected;
}
</script>

and the relevant HTML: 以及相关的HTML:

<table>
    <tr>
        <td>Event:</td>
        <td>
          <select name="Event" id="event" onChange="updateinput();">
            <option value="option1">option1</option>
            <option value="option2">option2</option>
            <option value="option3">option3</option>
            <option value="option4">option4</option>
            <option value="option5">option5</option>
            <option value="option6">option6</option>
            <option value="option7">option7</option>
          </select>
</td>
    </tr>
    <tr>
        <td>Starting Time:</td>
        <td><input type="text" name="dateTime"/></td>
    </tr>
    <tr>
        <td>Level Range:</td>
        <td><input type="text" size="3" maxlength="3" id="minLvl"> - <input type="text" size="3" maxlength="3" id="maxLvl"></td>
    </tr>
    <tr>
        <td>Notes:</td>
        <td><textarea></textarea></td>
    </tr>
    <tr>
        <td>Create Event:</td>
        <td><input type="button" value="Create event!"></td>
    </tr>
</table>

I know I can use e.options[e.selectedIndex].text to get the text and place it in the input, but is there a way I could add attributes to each option so its like <option value="option1" minLvl="1" maxLvl="10">Option1</option> and then pull the data from minLvl and maxLvl and then populate the values into the inputs? 我知道我可以使用e.options[e.selectedIndex].text获取文本并将其放置在输入中,但是有一种方法可以为每个选项添加属性,因此它类似于<option value="option1" minLvl="1" maxLvl="10">Option1</option> ,然后从minLvlmaxLvl提取数据,然后将值填充到输入中?

function updateinput(){
var e = document.getElementById("event");
var catSelected = this.value;

document.getElementById("minLvl").value=catSelected;
}

<select name="Event" id="event" onChange="updateinput(this);">

I was able to get it to work with this code: 我能够将其与以下代码一起使用:

<script type="text/javascript">
function updateinput(){
var e = document.getElementById("event");
var minLvl = e.options[e.selectedIndex].getAttribute("minLvl");
var maxLvl = e.options[e.selectedIndex].getAttribute("maxLvl");

document.getElementById("minLvl").value=minLvl;
document.getElementById("maxLvl").value=maxLvl;
}
</script>

<option value="option" minLvl="1" maxLvl="3">option</option>

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

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