jQuery为select元素添加新选项不会获取字符串

时间:2013-10-30 20:27:55

标签: jquery html css string select

我正在尝试为每个选择元素添加一个新选项,并让它显示选项值,就像'US California'显示的那样,例如我添加了一个选项到US-select但是在选中时,'US California'仍然显示。任何想法为什么?

这是一个js小提琴来演示我的意思:http://bit.ly/1aU1n5E

谢谢

1 个答案:

答案 0 :(得分:0)

变化:

var str = $('#country-select').val() + " " + $("#" + $("#country-select option:selected").val() + "-select option:selected").val();

为:

var str = $('#country-select, .sub-menu').val() + " " + $("#" + $("#country-select option:selected").val() + "-select option:selected").val();

<强> jFiddle example

您需要在第一次和第二次选择时触发更改,因此需要$('#country-select, .sub-menu')