简体   繁体   English

用户在输入=文本中按下Enter时的jQuery函数commit(),也提交位于同一表格中的输入=提交

[英]JQuery function submit() for when the user presses Enter in an input=text submitting also an input=submit located in the same form

so I have a form with two inputs: an input=text and an input=submit . 所以我有一个带有两个输入的表单: input = textinput = submit The input text is used to update the item's info and the input submit to delete the item. 输入的文本用于更新项目的信息,输入的提交用于删除项目。 The item's info is updated after the user edit the info in the input text and press Enter key. 用户在输入文本中编辑信息并按Enter键后,该项目的信息将更新。 I'm using JQuery for this, see the code below: 我为此使用JQuery,请参见下面的代码:

In JS file 在JS文件中

  $(".input-edit").focus();
    $(".input-edit").keyup(function (event) {
    if (event.keyCode == 13) {
        $(currentEle).html($(".input-edit").val());
        this.form.submit();
        return false;
    }
});

It is working fine, but the PROBLEM IS : when the user presses Enter the input submit (delete button) is also submitted and after updating the info it delete the item. 它工作正常,但问题是 :当用户按下Enter键时,也会提交输入的提交(删除按钮),并且在更新信息后删除该项目。 How can I only submit the update when the user presses Enter key? 如何仅在用户按下Enter键时提交更新?

The HTML form (index.php): HTML表单(index.php):

  <form action="crud.php" method="post">
      <input class="input-edit" name="editInfo" type="text" value="$item['info']" />
      <input type="hidden" name="id" value="<?php echo $item['id']?>">                

      <button class="btn" type="submit" name="delete">
          <i class="icon-deletar material-icons right">delete</i>
      </button>                            
  </form>

The PHP code for update and delete (crud.php): 用于更新和删除的PHP代码(crud.php):

if(isset($_POST['editInfo'])){
    $info= trim($_POST['editInfo']);
    $id= $_POST['id'];

    if(!empty($info)){
        $query= $db->prepare("UPDATE item SET info = '$info' WHERE id = $id");
        $query->execute();
    }

}else if(isset($_POST['delete'])){

    $id= $_POST['id'];
    if(!empty($cod)){
        $query= $db->prepare("DELETE FROM item WHERE id = $id");
        $query->execute();
    }
}

May I be mistaken in thinking you are using bootstrap (as you are using the class btn ) ? 我可能会以为您正在使用引导程序(因为您正在使用btn类)而弄错了吗?

Regardless, you could change your delete button to a link that points to the delete operation. 无论如何,您都可以将删除按钮更改为指向删除操作的链接。 (You'll need to replace [url] ) This way when you click enter, it will not fire any submit button. (您需要替换[url] )这样,当您单击Enter时,它不会触发任何提交按钮。

index.php index.php

<a class="btn" href="[url]?delete=true&id=<?php echo $item['id'] ?>">
    <i class="icon-deletar material-icons right">delete</i>
</a> 

crud.php crud.php

} else if (isset($_GET['delete'])){
    $id= $_GET['id'];
    if (!empty($cod)) {
        $query= $db->prepare("DELETE FROM item WHERE id = $id");
        $query->execute();
    }
} 
<form action="crud.php" method="post">
  <input class="input-edit" name="editInfo" type="text" value="$item['info']" />
  <input type="hidden" name="id" value="<?php echo $item['id']?>">                

  <input type="hidden" name="isDelete" value="0">
  <button class="btn" type="button" name="delete">
      <i class="icon-deletar material-icons right">delete</i>
  </button>                            
</form>

And you have to write custom script for OnClick event of delete button, to set isDelete = 1 and submit form. 并且您必须为删除按钮的OnClick事件编写自定义脚本,以设置isDelete = 1并提交表单。 But better you can use a link for delete button 但是更好的是您可以使用链接来删除按钮

Don't make the button a submit button. 不要将按钮设为提交按钮。 Just have it be a regular button that invokes the PHP code via an AJAX call: 只是将其作为通过AJAX调用来调用PHP代码的常规按钮即可:

 $(function(){ $(".btn").on("click", function(){ $.ajax({ url: "crud.php", data: "" // The data to send }).done(function() { // Your success callback code here }); }); }); $(".input-edit").focus(); $(".input-edit").keyup(function (event) { if (event.keyCode == 13) { $(currentEle).html($(".input-edit").val()); this.form.submit(); return false; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="crud.php" method="post"> <input class="input-edit" name="editInfo" type="text" value="$item['info']" /> <input type="hidden" name="id" value="<?php echo $item['id']?>"> <button class="btn" type="button" name="delete"> <i class="icon-deletar material-icons right">delete</i> </button> </form> 

It is possible to disable fields before the form is submitted which will eliminate them from the data submitted by the web browser. 可以在提交表单之前禁用字段,这会将其从Web浏览器提交的数据中删除。

$(document).ready(function() {
    $(".input-edit").focus();
    $(".input-edit").keypress(function (event) {
      if (event.keyCode == 13) {
        $(this).html($(".input-edit").val());
        $('button[name="delete"]').prop('disabled', true);
        this.form.submit();
        event.preventDefault();
        return false;
      }
    });
});

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

相关问题 禁用提交表单jquery的提交输入 - Disable a submit input from submitting a form jquery 当用户点击输入只有一个输入的表单时,请避免“提交” - avoid “submit” when user hits enter in form with only one input 在输入文本字段上按Enter提交表单 - Press enter on input text field to submit form 当焦点不在输入中时,允许在ENTER上提交FORM - Allow FORM submit on ENTER when focus is not in an input 两个文本输入,一个提交按钮。 提交表单时,使用jQuery或JS在其他输入中显示第一个输入的值? - Two text inputs, one submit button. When submitting the form, value of first input appears in the other input with jQuery or JS? 使用锚点作为提交输入时,在输入/返回上提交表单 - Submitting forms on enter/return when using anchors as submit-input 用户按下输入时如何调用函数 - how do I call function when the user presses enter on the input 当用户按下回车键时,输入框是否可以运行功能? - Is there a way for a input box to run a function when the user presses the enter key? 当表单具有“提交”输入时,从输入(文本)字段中击中“输入” - Hitting “enter” from an input (text) field when a form has a “submit” input 通过jQuery提交表单删除提交输入变量 - submitting a form via jquery removes submit input variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM