我必須創建一個輸入文本框和選擇框。在select中,選項將包含一些userId值是用戶名。用戶可以使用文本框和選擇框來輸入數據。如果他/她選擇了任何用戶名,那麼userId應該在文本框中更新。反之亦然,如果用戶在文本框中輸入userId userId應在文本框中更新。jquery選擇選項應根據選項編號選擇
功能的第一部分工作正常。
我沒有得到任何jQuery解決方案,如何使特定的選項ID選擇。
第二部分
這些動態地選擇和輸入框創建。這些將會增加。我需要爲每個人做。我如何爲使用jQuery的人做同樣的功能。
<input type="text" id="demoInput" />
<select id="demoSelect" >
<option id="0">User Name A</option>
<option id="1">User Name B</option>
<option id="3">User Name C</option>
<option id="4">User Name D</option>
<option id="5">User Name E</option>
<option id="6">User Name F</option>
</select>
$('#demoSelect').change(function(){
//alert('event fired');
var id = $(this).children(":selected").attr("id");
//alert('id is ' +id);
$('#demoInput').val(id);
});
$('#demoInput').change(function(){
alert('event fired');
$('#demoSelect').children(":selected").removeAttr("selected");
alert('id is ' +id);
$('#demoInput').val(id);
});
我創建了this
JS小提琴。