gpt4 book ai didi

jquery - 按回车键时ajax提交表单的问题

转载 作者:行者123 更新时间:2023-12-01 02:47:45 25 4
gpt4 key购买 nike

我正在尝试在输入字段中提交一些文本并将其输出到同一页面上而不刷新页面。然而,它并没有做我想做的事。我也有一个占位符元素,所以如果我不小心在字段中输入任何内容,它不会将我的占位符文本插入到我的数据库(mysql)中。我遇到的问题是,当我按 Enter 时,它只会刷新页面,并且当我尝试在成功时收到警报时,它不会提醒我。

--------------------下面的jquery ------------------

$('[placeholder]').parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}

});

var input = $('input.to_do').val();

dataList = 'list=' + input;
$.ajax({
type: "POST",
url: "ajax_table_edit.php",
data: dataList,
cache: false,
success: function(html)
{
alert(input);
//$('.to_do_list_').html(data);
//$('.n').hide();
}
});
});

--------------------------html如下-------------------- -

<form action='#' method='post' >
<input type="text" class="to_do shadow" placeholder="enter text"/>
</form>

感谢您的宝贵时间!

最佳答案

防止默认事件行为:

$('[placeholder]').parents('form').submit(function(event) {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}

});

var input = $('input.to_do').val();

dataList = 'list=' + input;
$.ajax({
type: "POST",
url: "ajax_table_edit.php",
data: dataList,
cache: false,
success: function(html)
{
alert(input);
//$('.to_do_list_').html(data);
//$('.n').hide();
}
});

event.preventDefault();
});

关于jquery - 按回车键时ajax提交表单的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7383756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com