可以'当第一个select的值改变时,让jquery设置select输入的值
Can't get jquery to set value of select input when value of first select changes
当我更改另一个选择输入的值时,我正在尝试更新一个选择输出的值。我无法在页面上发生任何事情,我想确保我在这段代码中没有语法错误或其他愚蠢的事情。
<div class="group">
<div class="inputs types">
<strong style="font-size:13px;">Category:</strong>
<select name="id" id="ctlJob">
<option value="1">Automotive</option>
<option value="2">Business 2 Business</option>
<option value="3">Computers</option>
<option value="4">Education</option>
<option value="5">Entertainment & The Arts</option>
<option value="6">Food & Dining</option>
<option value="7">Government & Community</option>
<option value="8">Health & Beauty</option>
<option value="9">Home & Garden</option>
<option value="10">Legal & Financial Services</option>
<option value="11">Professional Services</option>
<option value="12">Real Estate</option>
<option value="13">Recreation & Sports</option>
<option value="14">Retail Shopping</option>
<option value="15">Travel & Lodging</option>
</select>
<select name="type" id="ctlPerson"></select>
</div>
</div>
<script>
$(function() {
$("#ctlJob").change(function() {
//Get the current value of the select
var val = $(this).val();
$('#ctlPerson').html('<option value="123">ascd</option>');
});
});
</script>
尝试改用append
:
$(function() {
$("#ctlJob").change(function() {
//Get the current value of the select
var val = $(this).val();
var ctl = $('#ctlPerson').append('<option value="123">'+val+'</option>')[0].options;
ctl.selectedIndex = ctl.length-1;
});
});
http://jsfiddle.net/J69q8/
我认为您还需要设置"selected"属性。添加
$('#ctlPerson option[value="123"]').attr('selected', 'selected');
直到脚本的末尾。您当前正在将选项添加到选择列表中,但没有更改选择列表以显示它。
<div id="test">
<select name="sel" id="sel">
<option name="1" id="1" value="1">Automotive</option>
<option name="2" id="1 value="2">Business 2 Business</option>
<option name="3" id="1 value="3">Computers</option>
</select>
<select name="sel2" id="sel2"></select>
</div>
<script>
$("#sel").change(function() {
var val = $(this).val();
$('#sel2').html('<option value="1">NEW</option>');
)};
</script>
这适用于你需要做的事情。它有点像
相关文章:
- jquery点击函数select&取消选择
- Jquery:未触发select事件
- jQuery动态表单显示在select选项上
- 更改基于它的Select OPTION's INDEX(Jquery)
- JQuery 遍历当前 SELECT 值
- 带有select的jquery提交表单不起作用
- jquery select()/window.get选择仅在鼠标抬起后更新
- jQuery:根据select选项中的每页项目进行分页
- 在IE9中使用jQuery.change定位select元素
- 使用jquery验证函数不适用于select元素
- 使用JQuery获取select标记的innerHTML的值
- 可以't使用jquery获取表单中select字段的值
- 如何对jQuery屏蔽文本输入执行.select()
- 使用jQuery在select选项上设置HTML数据属性
- 带文本区域的jQuery SELECT
- jQuery val() 返回一个在 select/option 元素上带有 IE8 的数组
- jQuery未能将JSON数据附加为select选项
- 如何获取select jquery对象的选定选项的文本值
- 警告SELECT-JQUERY中的所有选定值
- 使用grep加载select jquery中的选项