0

我希望根据下拉列表的选择填写 2 个字段。这是我现在使用的代码(在我的情况下有效但不起作用):

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

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

和相关的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>

我知道我可以e.options[e.selectedIndex].text用来获取文本并将其放入输入中,但是有没有办法可以为每个选项添加属性,就像<option value="option1" minLvl="1" maxLvl="10">Option1</option>然后从中提取数据minLvlmaxLvl然后将值填充到输入中?

4

2 回答 2

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

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

<select name="Event" id="event" onChange="updateinput(this);">
于 2013-01-07T21:47:29.637 回答
0

我能够让它与这段代码一起工作:

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

于 2013-01-07T22:55:07.087 回答