![](/img/trans.png)
[英]How do I populate an html text input field based on an html select option
[英]How do I populate value to second input field based on a onchange event on a 'select' input?
我希望使用Javascript將選擇輸入的值復制到第二個輸入字段。 我嘗試使用onchange事件調用來做到這一點。 為了在select函數中更改時填充輸入字段id ='sort',js代碼應該是什么?
這是我的代碼
<input id = "sort">
<select name="sort_select" onchange="document.getElementById('sort').value = ('#sort_select').val();">
<option value="Ascending"> Ascending</option>
<option value="Descending"> Descending</option>
</select>
這不起作用。 我看到錯誤在於賦值(('#sort_select')。val();)這樣做的正確方法是什么?
JS解決方案: FIDDLE改變了這個:
<select name="sort_select" onchange="document.getElementById('sort').value = ('#sort_select').val();">
至
<select name="sort_select" onchange="document.getElementById('sort').value = this.value;">
onChange="document.getElementById('sort').value = (this.options[this.selectedIndex]).value;"
不要使用那個大丑線作為內聯JS。 另外,首先填充輸入框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.