简体   繁体   English

从Enter提交到jQuery,无需提交按钮,无需表单操作

[英]Submit to jQuery from on enter without Submit button without form action

I have looked at many similar questions, but perhaps I do not understand how to call my function. 我看过许多类似的问题,但是也许我不明白如何调用函数。

My goal is to allow the user to type in the form field and hit enter to submit. 我的目标是允许用户在表单字段中键入内容,然后按Enter键提交。

Currently if someone enters a value into the form it does nothing if you click enter. 当前,如果有人在表单中输入值,则单击Enter不会执行任何操作。 To execute they have to click the Search Inventory Button. 要执行,他们必须单击“搜索库存”按钮。

My search form simplified...there are many fields, all with a class of jquerydata. 我的搜索表单简化了……有很多字段,所有字段都带有一类jquerydata。 The class inventory_search_db_connect is bound to a jQuery function that gathers everything with jquerydata as a class and does its thing. 类stockstock_search_db_connect绑定到jQuery函数,该函数以jquerydata作为类来收集所有内容并执行其操作。

<form method="post"  action="" name="form3" id="form3">
<input type=text name="title" size=25 value="" class="jquerydata">
<input type=button class="inventory_search_db_connect" value="   Search Inventory" class="jquerydata">
</form>

The javascript/jQuery javascript / jQuery

$(document).ready(function () {
pageBinds.db_actions();
SearchBinds.search_actions();
});


var pageBinds = {

db_actions: function() {
var that = this;
 $('.inventory_functions_db_connect').on(
      'click', function(event){


          var query= $(this).closest('.parent').find('.jquerydata').serialize();

....does stuff here
  );
},
unbind: function() {
$('.inventory_functions_db_connect').off();
this.db_actions();
}

}

I tried several things, the one I though might work was 我尝试了几件事,虽然我可能会做的是

$(document).ready(function() {

$('#form3').on("submit", function() {
    this.db_actions();
});
});

On the form tag I added 在我添加的表单标签上

<form method="post"  action="" name="form3" id="form3" onsubmit="return false">

Since I am here, this obviously did not work. 因为我在这里,所以这显然行不通。

There are many other workarounds but I was unable to decipher how to call the function I want. 还有许多其他解决方法,但是我无法解释如何调用所需的函数。

Thanks in advance for any insights... Mike 预先感谢您的任何见解... Mike

In the html remove the onsubmit="return false" Because it is preventing to execute that 在html中删除onsubmit =“ return false”,因为它阻止了执行

$('#form3').on("submit", function() {
   this.db_actions();
});

Then in the JS you have to prevent the automatic submission by calleing preventDefault(): 然后在JS中,您必须通过调用preventDefault()来阻止自动提交:

$('#form3').on("submit", function(e) {
    e.preventDefault();
    this.db_actions();
 });

Edit: 编辑:

this.db_actions(); 

will not work because this is the DOM element so 将不起作用,因为这是DOM元素,因此

$('#form3').on("submit", function() {
   pageBinds.db_actions();
});

Let me know if this works ;) 让我知道这个是否奏效 ;)

try this : 尝试这个 :

$('input').keypress(function(e) { 
if (e.which = 13) 
{ 
this.db_actions();
}
});

Or 要么

$('input').keydown(function (e) {
    if (e.keyCode == 13) {
        this.db_actions();
    }
});

Or 要么

$("input").keypress(function (e) {
    if (e.which == 13) {
        e.preventDefault();
        $("form").submit();
    }
});

Thanks for the replies. 感谢您的答复。 @Alan Wagner led me in the right direction. @艾伦·瓦格纳(Alan Wagner)带领我朝着正确的方向前进。

I ended up adding a hidden submit button, with a class that is the same as the button that is clicked to submit. 我最终添加了一个隐藏的提交按钮,其类与单击提交的按钮相同。 This is bound to the function. 这绑定到该功能。

<input type="submit" class="inventory_search_db_connect" style="display: none;">

Then in the function I added 然后在我添加的功能中

event.preventDefault();

to stop the automatic submission 停止自动提交

Thanks, I hope this eventually helps someone else Mike 谢谢,我希望这最终可以帮助别人

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

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