最终我试图时option
被选中来运行select
的option
,的一些代码,依赖于(存储在任何属性,像rel
或数据等)的值。我有以下代码:jquery的变化()不接受选择
的JavaScript:
$('select#select1').change(function() {
set = $('#select1 option:selected').val();
alert(set);
// $('#select2').html()
});
HTML:
<select id="select1">
<option value="1" rel="1">First</option>
<option value="2" rel="2">Second</option>
<option value="3" rel="3">Third</option>
<option value="4" rel="4" selected="selected">Fourth</option>
</select>
这里包括两个问题。首先,上面的代码没有任何作用,因为change
函数似乎无法识别第一行上的$('select#select1')
选择器。如果我将它更改为$('select')
,它就可以工作。但是这并不好,因为在我的页面上有几个select
元素,我需要区分哪一个。其次,如果我通过$('select')
(并删除页面上的其他选择元素)暂时工作,那么无论我选择的4个选项中的哪一个我总是获得值“1”的提醒。
如果我使用$('#select1 option:selected').attr('rel');
代替,则会发生同样的情况。
为了上帝的爱,为什么?
在这里工作:http://jsfiddle.net/ZyBHe/ – r0m4n 2012-04-03 16:24:24
您的代码http://jsfiddle.net/skram/3xS9D/2/工作正常小提琴。 – 2012-04-03 16:41:52
@SKS - 我明白了。 Jeez,多么愚蠢。我有两个选择元素漂浮在右边,我点击第一个元素(在左边),认为它是我的代码中的第一个,当然它是最右边的元素。完全愚蠢。我需要去散步。非常感谢。 – Inigo 2012-04-03 16:45:55