admin管理员组文章数量:1414613
I have select
and button
:
<select id="s">
<option id="o1">O1</option>
<option id="o2">O2</option>
</select>
<button id="a">O1 -> O11</button>
I want to see O11
in select after pressing button:
$('#s').click(function(){
$('#o1').text('O11');
})
But select refreshes only after clicking on select.
/
I have select
and button
:
<select id="s">
<option id="o1">O1</option>
<option id="o2">O2</option>
</select>
<button id="a">O1 -> O11</button>
I want to see O11
in select after pressing button:
$('#s').click(function(){
$('#o1').text('O11');
})
But select refreshes only after clicking on select.
https://jsfiddle/dfwbLsn1/
Share Improve this question edited Sep 14, 2022 at 12:00 aynber 23k9 gold badges54 silver badges68 bronze badges asked Feb 25, 2016 at 12:12 Ivan BorshchovIvan Borshchov 3,6605 gold badges44 silver badges65 bronze badges2 Answers
Reset to default 4You must bind the click
event to the button:
$('#a').click(function(){
$('#o1').text('O11');
});
That should do it.
Edit: Based on your ment, you also want to change the selected option with the button click. Note that you have no mention of this in your question.
To change the selected option, the options need values. This is how to achieve what you're after:
<select id="s">
<option id="o1" value="1">O1</option>
<option id="o2" value="2">O2</option>
</select>
<script>
$('#a').click(function() {
$('#o1').text('O11');
$('#s').val(1);
});
</script>
Now clicking the <button>
also changes the value of the <select>
.
You are binding click event with select click.
$('#s') //this is you are binding with select
If you want to do something on button click, then you have to bind click event with button
$('#a').click(function(){
$('#o1').text('O11');
})
本文标签: javascriptRefresh select using jqueryStack Overflow
版权声明:本文标题:javascript - Refresh select using jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745184160a2646602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论