Javascript onchange - 使用 jquery 选择的下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10491724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onchange - the dropdown selected using jquery
提问by Nick Kahn
i have a dropdown in the header and whatever the value is selected in the header should reflect the same in the detail dropdown, how should i do that? any leads?
我在标题中有一个下拉列表,在标题中选择的任何值都应该在详细信息下拉列表中反映相同的内容,我应该怎么做?任何线索?
$("#myddl").change(function()
{
//update all the dropdown list...
});
http://jsfiddle.net/abuhamzah/4QvfP/
http://jsfiddle.net/abuhamzah/4QvfP/
Header:
<br />
<select id="myddl" name="myddl">
<option value="1">One</option>
<option value="2">Twooo</option>
<option value="3">Three</option>
</select>
<p>Detail</p>
<br />
<select id="Select1" name="myddl">
<option value="1">One</option>
<option value="2">Twooo</option>
<option value="3">Three</option>
</select>
<br />
<select id="Select2" name="myddl">
<option value="1">One</option>
<option value="2">Twooo</option>
<option value="3">Three</option>
</select>
<br />
<select id="Select3" name="myddl">
<option value="1">One</option>
<option value="2">Twooo</option>
<option value="3">Three</option>
</select>
<br />
<select id="Select4" name="myddl">
<option value="1">One</option>
<option value="2">Twooo</option>
<option value="3">Three</option>
</select>
<br />
<select id="Select5" name="myddl">
<option value="1">One</option>
<option value="2">Twooo</option>
<option value="3">Three</option>
</select>
采纳答案by Nathan Taylor
Something along these lines will work. It wraps your "Detail" selects in a container to make selection somewhat simpler.
沿着这些路线的东西会起作用。它将您的“详细信息”选择包装在一个容器中,使选择更加简单。
$("#myddl").change(function()
{
$('.detail').find('select').val($(this).val());
});?
回答by Sampson
Use $.on
to bind to the "change" event. From here we target all select
elements whose id
begins with Select
- which includes all of those below. Then we set their value(s) to that of the current one.
使用$.on
绑定到“改变”事件。从这里开始,我们针对以-开头的所有select
元素,其中包括以下所有元素。然后我们将它们的值设置为当前值。id
Select
?$("#myddl").on("change", function(o){
$("select[id^=Select]").val(o.target.value);
});??????????
回答by Boguslaw
?$(function(){
$('#myddl').change(function(){
var value = $(this).val();
$('select[name=myddl] option[value='+value+']').attr('selected', 'selected');
});
})?
回答by Leniel Maccaferri
This code will do it:
这段代码会做到:
$(document).ready(function()
{
$("#myddl").live('change', function()
{
$("#Select1, #Select2, #Select3, #Select4, #Select5").val($(this).val());
});
});
The updated JS Fiddle is here: http://jsfiddle.net/leniel/4QvfP/7/
更新的 JS Fiddle 在这里:http: //jsfiddle.net/leniel/4QvfP/7/
回答by ltiong_sh
In the onchange of the first dropdown, you can jQuery select the other dropdowns and use a .each() to iterate and set the other options.
在第一个下拉列表的 onchange 中,您可以 jQuery 选择其他下拉列表并使用 .each() 来迭代和设置其他选项。