如何使用jquery?
用select#1的选项值填充我的select#2例如,选择#1是:
<select id="#1">
<option value="1">Val 1</option>
<option value="2">Val 2</option>
<option value="3">Val 3</option>
我的选择#2应该是这样的:
<select id="#2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
答案 0 :(得分:3)
<强> Fiddle DEMO 强>
如果您将其更改为正确的id
,我可以在下面进行操作。
$(document).ready(function(){
$("#2").append($('#1').html()); //set options of 2nd select from options of 1st select
$("#2 option").each(function(){
$(this).text($(this).val());//set its text based on its value
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="1">
<option value="1">Val 1</option>
<option value="2">Val 2</option>
<option value="3">Val 3</option>
</select>
<select id="2">
</select>