【问题标题】:Undo dropdown selection撤消下拉选择
【发布时间】:2014-10-07 21:38:46
【问题描述】:

请有人告诉我为什么“return false”语句在以下情况下不起作用:

http://jsfiddle.net/mattkoen/8149m5kg/

<select id="userTypes" title="Choose a user type">
    <option value="0">Choose a user type</option>
    <option value="superu">Super U</option>
    <option value="admin">Admin</option>
</select>

$('#userTypes').change(function () {

    if (!confirm("Are you sure?")) {
        return false;
    }
});

【问题讨论】:

标签: jquery html drop-down-menu


【解决方案1】:

返回false 不会撤消选择,而是同时充当event.preventDefaultevent.stopPropogation。相反,你需要这个。

if (!confirm("Are you sure?")){
    $(this).val($(this)[0].initialValue); // now it undos the select.
}

DEMO

【讨论】:

  • 关闭,但将其更改为初始值,而不是 last 选定值(撤消)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
相关资源
最近更新 更多