任何知道如何传递 a/b/c 值的人。如果选择为王,则在不同的文本框中传递值 a b 和 c。如果是 queen,则将值 d、e 和 f 传递到同一个文本框。任何人都可以帮助我实现可以满足我的需要的功能吗?
<select id="select" name="select" >
<option value="">---Please select---</option>
<option value="a/b/c">king</option>
<option value="d/e/f">queen</option>
</select>
<input type="text" class="form-control" id="a" name="a" value=a required>
<input type="text" class="form-control" id="b" name="b" value=b required>
<input type="text" class="form-control" id="c" name="c" value=c required>
最佳答案
向您的选择元素添加 onchange 事件;
<select id="select" name="select" onchange="fn()">
<option value="">---Please select---</option>
<option value="a/b/c">king</option>
<option value="d/e/f">queen</option>
</select>
然后编写以下 JavaScript 来实现目标:
function fn() {
var x = document.getElementById("select").value;
var arr_x = x.split('/');
document.getElementById("a").value = arr_x[0];
document.getElementById("b").value = arr_x[1];
document.getElementById("c").value = arr_x[2];
}
关于JavaScript:分割函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43842727/