简体   繁体   English

Javascript / jQuery:同时检测返回键按下和提交按钮按下

[英]Javascript/jQuery : Detect both return key press and submit button press simultaneously

I have an input field and an "add" button below it. 我有一个输入字段,下面有一个“添加”按钮。 What the add button basically does is, it appends a new input field to the document. 添加按钮的主要作用是将新的输入字段添加到文档中。 What I am trying to do here is, while typing in an input field if return key press is detected the function that calls the addition of new input field is fired the same function that is fired when the add button is clicked. 我要在这里做的是,如果在输入字段中键入内容(如果检测到返回键按下),则会触发调用添加新输入字段的功能,而单击添加按钮时将触发相同的功能。

Can I incorporate the detection of return key press in the following somehow? 我可以通过以下方式结合使用回车键检测吗?

$('.addNewSite').on('click', function(){


          var idNewInput = "site" + numInputFields;
          $('.inputFieldsSettingsPanel').append('<input type="text" class="tracksiteInput" id = "' + idNewInput + '"></input>');
          $("#" + idNewInput).focus();

});

I think you want this http://jsfiddle.net/naeemshaikh27/cy84t4pz/ 我想你想要这个http://jsfiddle.net/naeemshaikh27/cy84t4pz/

function fun(){
$('body').append('<input type="text"/>');
}

$('.addButton').on('click', fun);

$('body').on('keypress','input', function(e){
if (e.keyCode == 13) {
       fun();
    }    
});

something like this? 这样的东西? e.which in the keypress() is what you're looking for to see what button is pressed. e.which keypress()中的哪个是您要查看的内容,以查看按下了什么按钮。 in this case, 13 is equivalent to the enter key 在这种情况下, 13等效于Enter键

 $(document).ready(function() { $('.add').on('click', function() { var html = '<input type="text" class="field" /><br/><br/>'; $('.form').append(html); }); $(document).on("keypress", ".field", function(e) { if(e.which == 13) { $('.add').trigger('click'); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <a class="add" href="#">add</a><br/><br/> <div class="form"> <input type="text" class="field" /><br/><br/> <input type="text" class="field" /><br/><br/> </div> 

You cant detect enter keypress on the input and trigger the button's click event. 您无法检测到输入上的Enter键按下并触发按钮的click事件。

 $("button").on("click", function(e){ $("body").append($("<input>").attr("type", "text")); }); $(document).on("keypress", "input", function(e){ if(e.which == 13) $("button").trigger("click"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Add Input</button> 

 var i; var inputs=document.getElementsByClassName('add'); for(i=0;i<inputs.length;i++){ inputs[i].addEventListener('keyup',function(event){ if(event.keyCode){if (event.keyCode=="13" ){createNewElem(); }} //if(event.which){if(event.which =="13"){alert('return pressed');}} }); } function createNewElem(){ var newinput=document.createElement('input'); var inptype=document.createAttribute('type'); inptype.value="text"; newinput.setAttributeNode(inptype); var inpclass=document.createAttribute('class'); inpclass.value="add"; newinput.setAttributeNode(inpclass); document.body.appendChild(newinput); } 
 <input type="text" class="add" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM