gpt4 book ai didi

jquery - 为什么我的表单在应该进行 ajax 调用时提交

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

当我提交表单时,我希望在控制台中进行 ajax 调用,并且页面 url 保持不变...但表单操作已执行,并且 http 调用更改了 url。

<head>
<script src="lib/jquery-1.6.4.min.js" type="text/javascript" ></script>
<script>
$(document).ready(function() {

$( '#myForm' ).submit( function() {
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
$('#resultsDiv').html(response); // update the DIV
}
return false;
});

});
</script>
</head>

<body>

<div id="myFormDiv" >
<form name="myform" id="myform" action="form_handeler.cfm" method="post" >
<input type="text" size="100" name="shoe" value="" /> <input type="submit" name="myFormSubmit" value="submit" />
</form>
</div>

<div id="resultsDiv" >

</div>

</body>

最佳答案

您的代码未正确用 }); 括起来,即您的脚本由于 }); 出现错误,因此您的脚本未按您的预期执行,因此默认表单提交发生。

   $(function() {
$('#myform').submit( function() {
$.ajax({ // create an AJAX call...
...
}); //HERE
return false;
});
});

关于jquery - 为什么我的表单在应该进行 ajax 调用时提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233652/

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