<select id="dd">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
//change option to 2; works
$("#dd").find('option[value=2]').attr("selected",true);
$("#dd").find('option[value=2]').attr("selected",false)
//change option to 3; works
$("#dd").find('option[value=3]').attr("selected",true);
$("#dd").find('option[value=3]').attr("selected",false);
//change option back to 2; doesn't work
$("#dd").find('option[value=2]').attr("selected",true);
更改值以支持之前设置的值不起作用.. 任何的想法?如果我做错了什么......
答案 0 :(得分:0)
这是你想要的吗?
$('#set2').click(function(){
$("#dd option[value=2]").prop("selected",true);
});
$('#set3').click(function(){
$("#dd option[value=3]").prop("selected",true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="dd">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<button id="set2">Set to 2</button>
<button id="set3">Set to 3</button>