gpt4 book ai didi

jquery - 提交表单后如何保持确认消息不变?

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

我正在尝试制作一个简单的时事通讯注册表单,其中包含电子邮件地址字段和提交按钮。当用户输入他们的地址并点击提交时,我希望表单下方显示一条确认消息。实际发生的情况是消息出现并立即消失。我怎样才能让它保持原状?这是我的代码:

<body>
<form id="newsletter_signup"> <!-- form action to be added later -->
<p><input type="text" name="email" id="email" placeholder="your@email.com" />
<input type="submit" value="Subscribe"></p>
<div id="feedback"></div>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script>
$('#newsletter_signup').submit(function() {
var email_address = $('#email').val();
$('#feedback').html('Thanks! ' + email_address + ' has been signed up.');
});
</script>

最佳答案

将代码更改为此,通过 Ajax 请求提交表单:

$('#newsletter_signup').submit(function(e) {
e.preventDefault();
$.post('newsletter_signup', function (data){
var email_address = $('#email').val();
$('#feedback').html('Thanks! ' + email_address + ' has been signed up.');
});
return false;
});

关于jquery - 提交表单后如何保持确认消息不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17491494/

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