Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 多个事件上的事件处理程序_Javascript_Jquery - Fatal编程技术网

Javascript 多个事件上的事件处理程序

Javascript 多个事件上的事件处理程序,javascript,jquery,Javascript,Jquery,可能重复: 我有以下功能: $("td.delivered").click(function() { $(this).html($("<input/>", { id: 'inp', style: 'width:80px;', placeholder: "YYYY-MM-DD", change: function() { selectdone(this, titl

可能重复:

我有以下功能:

$("td.delivered").click(function() {

        $(this).html($("<input/>", {
          id: 'inp',
          style: 'width:80px;',
          placeholder: "YYYY-MM-DD",
          change: function() {
            selectdone(this, title_id, status_type);
          },
          blur: function() {
            selectdone(this, title_id, status_type);
          },
          onkeypress=="Return": function() { // pseudo-code
            selectdone(this, title_id, status_type);
          }
        })
        );
}

如何更简洁地编写此代码,使
selectdone
函数启用
change
blur
return

您可以使用
bind

  $(this).html($("<input/>", {
      id: 'inp',
      style: 'width:80px;',
      placeholder: "YYYY-MM-DD",
      change: function() {
        selectdone(this, title_id, status_type);
      }
  });

  $(this).bind('blur keypress change', function(e){
     selectdone(this, title_id, status_type);
  });
$(this.html($(“”){
id:'inp',
样式:“宽度:80px;”,
占位符:“YYYY-MM-DD”,
更改:函数(){
选择完成(此项、标题\u id、状态\u类型);
}
});
$(this).bind('blur keypress change',函数(e){
选择完成(此项、标题\u id、状态\u类型);
});

您可能需要修改不同事件的代码。请注意,要知道当前触发了哪个事件,您可以使用
e.type

您可以使用
bind

  $(this).html($("<input/>", {
      id: 'inp',
      style: 'width:80px;',
      placeholder: "YYYY-MM-DD",
      change: function() {
        selectdone(this, title_id, status_type);
      }
  });

  $(this).bind('blur keypress change', function(e){
     selectdone(this, title_id, status_type);
  });
$(this).html($("<input/>", {
      id: 'inp',
      style: 'width:80px;',
      placeholder: "YYYY-MM-DD"
  });

$('input', this).bind('change blur keypress', function(e) {
    if(e.type != 'keypress' || e.keyCode == 13){
        selectdone(this, title_id, status_type);
    }
});
$(this.html($(“”){
id:'inp',
样式:“宽度:80px;”,
占位符:“YYYY-MM-DD”,
更改:函数(){
选择完成(此项、标题\u id、状态\u类型);
}
});
$(this).bind('blur keypress change',函数(e){
选择完成(此项、标题\u id、状态\u类型);
});
您可能需要修改不同事件的代码。请注意,要知道当前触发了哪个事件,您可以使用
e.type

$(this.html($)(“”{
$(this).html($("<input/>", {
      id: 'inp',
      style: 'width:80px;',
      placeholder: "YYYY-MM-DD"
  });

$('input', this).bind('change blur keypress', function(e) {
    if(e.type != 'keypress' || e.keyCode == 13){
        selectdone(this, title_id, status_type);
    }
});
id:'inp', 样式:“宽度:80px;”, 占位符:“YYYY-MM-DD” }); $('input',this).bind('change blur keypress',函数(e){ 如果(e.type!=“按键”| e.keyCode==13){ 选择完成(此项、标题\u id、状态\u类型); } });
$(this).html($(“”{
id:'inp',
样式:“宽度:80px;”,
占位符:“YYYY-MM-DD”
});
$('input',this).bind('change blur keypress',函数(e){
如果(e.type!=“按键”| e.keyCode==13){
选择完成(此项、标题\u id、状态\u类型);
}
});


这并不能解释伪“Return”函数的作用,但并不容易。您必须检查事件类型,然后查看它是否为“keypress”或其他类型,然后检查事件的keycode是否为“13”因为这是返回按钮的键码。这并不能解释它的伪“返回”功能,但不是那么容易。您必须检查事件类型,然后查看它是否为“按键”或其他类型,然后检查事件的键码是否为“13”因为这是返回按钮的键码。这不解释伪“返回”function@maxhud谢谢你,你能写出完整的函数吗?我会接受你的回答。这就是。这应该可以用的fine@maxhud:嗯,这实际上不起作用。如果一行的值为None,它将删除该行,而不是保留该值(鉴于我包含的上述函数正确地实现了这一点)。我对js的了解还不够,不知道为什么,但函数中的操作与我上面提供的操作不完全相同。试试看。我不小心将函数应用到了输入的容器,而不是每个输入。现在,绑定函数中的“this”将是容器中的任何输入,而不是容器中的任何输入。这不符合t表示伪“返回”function@maxhud谢谢你,你能写出完整的函数吗?我会接受你的回答。这就是。这应该可以用的fine@maxhud:hmm,因此这实际上不起作用。如果一行的值为None,它将删除该行,而不是保留该值(而我包含的上述函数正确地执行此操作)。我对js的了解不够,不知道为什么,但函数中的操作与我上面提供的操作不完全相同。试试看。我不小心将函数应用于输入的容器,而不是每个输入。现在,绑定函数中的“this”将是容器中的任何输入,而不是容器中的任何输入。为什么需要both模糊和更改?模糊覆盖正在更改的值或not@ianpgall如果用户单击另一个元素,Blur将对此负责,在这种情况下,我希望该功能启动。例如,如果您在StackOverflow中单击您的用户菜单,然后再次单击页面上的其他位置,用户菜单将消失。哦,我理解,但这意味着您不需要也不需要绑定“change”。所有“change”都是,当元素未聚焦且值已更改时。“blur”就是当一个元素没有焦点时。@ianpgall哦,我现在明白了。谢谢你的解释。没问题。这不太重要,但如果你同时使用模糊和更改来执行同一件事情,它可能是多余的。为什么你同时需要模糊和更改?模糊覆盖了一个正在更改或更改的值not@ianpgall如果用户点击Ano,Blur将对此负责r元素,在这种情况下,我希望函数启动。例如,如果您在StackOverflow中单击用户菜单,然后再次单击页面上的其他位置,用户菜单将消失。哦,我理解,但这意味着您不需要绑定“更改”。所有“更改”都是,当元素未聚焦且值已更改时。“模糊”就是当一个元素没有焦点时。@ianpgall哦,我现在明白了。谢谢你的解释。没问题。这不太重要,但如果你同时使用模糊和更改来执行同一件事情,它可能是多余的。