间隔上的自动POST表单缺少表单字段值



当使用按钮发布表单时,我有一个脚本可以很好地工作。但当我将表单转换为间隔(自动(发布时,发布时会丢失表单(字段(值
我知道这与使用最接近(表单(作为按钮有关,并以最接近(形式(作为参考,但Interval上的自动发布没有最接近(形状(的参考。感谢您的帮助。顺便说一下,我的表单是在sqlwhile循环中。

A( 是与按钮一起使用时的脚本。B( 是与Auto(post(on间隔一起使用时的脚本。

表单(

<form  class="updateform"  action="" method="" > 
<input type="hidden" class ="customerid"  name="" value="<?php echo $customerid?>">
<a   class ="test" >test</a>
<div class="update"> </div>
</form>

**A(**

<script>
$(document).ready(function(){
$('.test').click(function(event){
event.preventDefault();

var form = $(this).closest("form");
var field1= form.find('.customerid').val();

// Url to post to and Field name and content */          
$.post('/test4.php', {customerid:field1},

// Alert Success
function(data){
// Alerts the results to this Div
form.find('.update').html(data);

});
});
});
</script>  

**b(**

<script>
$(function() {
var form = $(this).closest("form");
var field1= $('.customerid').val();
function update() {
$.post("/test4.php",  {customerid:field1},
function(data){
$('.update').html(data); 
});
}
setInterval(update, 3000);
update();
});
</script>

这可能是因为第二个脚本中的.closest()方法。您正在尝试选择树上最接近this的窗体。但在这种情况下,this将是函数的作用域或window对象。将其更改为jQuery选择器并选择正确的表格。

您可以通过使用console.logdebugger语句来检查浏览器的开发工具中元素的值,从而自行进行测试。熟悉这些人,你会非常需要他们,即使是最有经验的开发人员,他们也是必不可少的工具。

如果我可以给个提示的话。使用$前缀命名jQuery元素(使用$('element')选择的元素(。通过这种方式,你可以通过读取变量来知道该变量有什么样的值。此外,给变量起一个有意义的名字会让它更容易阅读和理解。field1是通用的,但customerIdField告诉它是什么字段

$(function() {
var $form = $('form.updateform');
var $updateField = $form.find('.update')
var $customerIdField = form.find('.customerid');
var customerIdValue = $customerIdField.val();
function update() {
$.post("/test4.php",  {customerid: customerIdValue}, function(data) {
$updateField.html(data); 
});
}
setInterval(update, 3000);
update();
});

最新更新