【问题标题】:What event fires when item in HTML select/dropdown list is selected?选择 HTML 选择/下拉列表中的项目时会触发什么事件?
【发布时间】:2013-07-01 21:59:31
【问题描述】:

我想响应用户在选择元素中选择项目。然而这个 jQuery:

$('#platypusDropDown').select(function () {
    alert('You selected something');
});

...什么都不做。尽管 jsFiddle 认为它是有效的 jQuery,但它没有显示任何警报。

Click 事件有效,但它太快了 - 在进行选择之前,它会在单击选择元素时触发。

当然,我真的很想做这样的事情:

$('#platypusDropDown').select(function () {
    var selection = $('platypusDropDown').val;
    $.getJSON('platypus.json', selection, data() {
        // . . .
    });
});

HTML:

<select id="platypusDropDown">
    <option value="duckbill">duckbill</option>
    <option value="duckbillPlatypus">duckbillPlatypus</option>
    <option value="Platypus">Platypus</option>
    <option value="Platypi">Platypi</option>
</select>

【问题讨论】:

    标签: javascript html jquery


    【解决方案1】:

    你需要使用change事件

    $('#platypusDropDown').change(function () {
        var selection = this.value; //grab the value selected
        $.getJSON('platypus.json', selection, data() {
        . . .
        });
    });
    

    Fiddle

    加上$('platypusDropDown').val这里val应该是val()并且你不需要在元素上再次创建一个jquery对象,处理程序内部的this代表DOM元素(选择事件绑定到的元素),你可以使用this.value$(this).val() 直接访问值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      相关资源
      最近更新 更多