【问题标题】:How to when enter is hit the save button in jquery fires?如何在输入时点击 jquery 触发中的保存按钮?
【发布时间】:2014-02-17 20:20:40
【问题描述】:

这是我添加客户记录的 jquery 代码:

    $("#dialog-form").dialog({
    autoOpen: false,        //Shows dialog
    height: 300,
    width: 220,
    modal: true,
    buttons: {
      "Save": function () {
        $(this).dialog('close');
          alert("Customer successfully added!");
           $.post('insertcus.php', $('#customer').serialize(), function(result){




});     

        },
        "Cancel": function () {
            $(this).dialog("close");
        }



    }


});

按下回车键时如何自动点击保存按钮? 保存后如何自动刷新一个字段? 我想刷新我的输入选择字段。

【问题讨论】:

  • 使用.trigger()方法
  • $("#dialog-form").dialog({...}); 引用的对话框实际使用的插件是什么?因为 jqmodal 没有这样的方法。
  • 如?我还有其他选择吗?

标签: javascript jquery modal-dialog jqmodal


【解决方案1】:
$("#dialog-form").keypress(function(e) {
    if (e.keyCode == $.ui.keyCode.ENTER) {
           $('#saveButton').trigger('click');
    }
});

【讨论】:

    【解决方案2】:
    $(document).keypress(function(e) {
        if(e.which == 13) {
            $('#idSaveButton').trigger('click');
        }
    });
    

    【讨论】:

      【解决方案3】:

      如果#dialog-form 是一个表单

      $("#dialog-form").submit(function(ev) {
          ev.preventdefault();
          $('#idSaveButton').trigger('click'); // or whatever the save button id might be
      });
      

      实际上,将保存功能放在一个通用回调中会更简洁,以便表单提交和按钮单击事件共享它。

      【讨论】:

      • jquery 按钮没有 ids
      • 您应该对这两个事件使用相同的回调(在对话框中单击“保存”并提交表单)
      猜你喜欢
      • 1970-01-01
      • 2017-06-20
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多