gpt4 book ai didi

javascript - Ajax 发送 get 到原始页面而不是 post 到不同的页面

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

我有一个表单,当我提交它时,我想将结果页面加载到原始页面上的一个 div 中。当我单击表单的提交按钮时,它正在向原始页面发送获取请求,例如:http://localhost/hr/index_admin_test.php?posteddate=01-10-2015而不是向 http://localhost/hr/attendanceform2.php 发送请求

在原始页面中我有以下脚本:

 <script>
$('#timesheetsearch form').submit(function(){
var data=$(this).serialize();
// post data
$.post('attendanceform2.php', data , function(returnData){
$('#timesheets').html( returnData)
})

return false; // stops browser from doing default submit process
});
</script>

在页面正文中,我有以下表格和 div:

    <div class="content_text" id="timesheetsearch">
<p> Select the date you wish to view time sheets for:</p>

<p><form name="timesheetsearch"> <br><input name="posteddate" value="01-10-2015" id="datepicker" />
<div id="timesheets"></div>
<input type="submit"> </form> </p>

最佳答案

像这样把脚本放在表单后面

    <div class="content_text" id="timesheetsearch">
<p> Select the date you wish to view time sheets for:</p>

<p><form name="timesheetsearch"> <br><input name="posteddate" value="01-10-2015" id="datepicker" />
<div id="timesheets"></div>
<input type="submit"> </form> </p>

<script>
$('#timesheetsearch form').submit(function(){
var data=$(this).serialize();
// post data
$.post('attendanceform2.php', data , function(returnData){
$('#timesheets').html( returnData)
})

return false; // stops browser from doing default submit process
});
</script>

如果您在加载表单之前运行您的脚本,它将无法工作,因为脚本没有找到任何表单。你也可以在里面写你的脚本

$(document).ready(function(){

})

如果你真的想让你的脚本放在表单之前。希望您的脚本现在可以正常工作。

关于javascript - Ajax 发送 get 到原始页面而不是 post 到不同的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33452615/

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