当用户在表单中使用SELECT元素时,jQuery会检测到

时间:2010-09-30 14:56:05

标签: jquery html forms html-select

我希望能够检测用户何时从表单中的SELECT元素中选择了一个选项。我知道该怎么做,但不知道如何实现初步检测。

<select id="mySelect">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

如果可能,我想在JQuery中使用解决方案。

5 个答案:

答案 0 :(得分:4)

可能想要.change()处理程序,如下所示:

$("#mySelect").change(function() {
  var newVal = $(this).val();
  //do something
});

.val()获取下拉列表的当前值(作为字符串),只需使用它来做任何你想做的事情,例如alert("The new value is: " + $(this).val());等等。

答案 1 :(得分:1)

$("#mySelect").bind("change", function(event){ ... });

答案 2 :(得分:1)

或只是使用

$("#mySelect").change(function(){
  //do something here
})

答案 3 :(得分:0)

$('#mySelect').change(function() {
  alert('Handler for .change() called.');
});

答案 4 :(得分:0)

jQuery('#mySelect').bind('change',function(e){

        //value of selected item
        var _selectedValues=jQuery(this).val();

        //your codes  here
});

我更喜欢bind over .change(),. click()等:因为bind比其他更通用。