[英]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 = text和input = 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.