Jquery 将值传递给ajaxform数据

Jquery 将值传递给ajaxform数据,jquery,Jquery,我想将变量的值传递给ajaxForm数据 value1 = "dynamic_value1"; value2 = "dynamic_value2"; $('form').ajaxForm({ data: { key1: value1, key2: value2 } }); 期待类似于: date:{Key1:"dynamic_value1", Key2:"dynamic_value2"} 因此,在php中,我可以访问 echo $_POST['ke

我想将变量的值传递给ajaxForm数据

value1 = "dynamic_value1";
value2 = "dynamic_value2";
$('form').ajaxForm({
    data: {
      key1: value1, 
      key2: value2 
    }
});
期待类似于:

date:{Key1:"dynamic_value1", Key2:"dynamic_value2"}
因此,在php中,我可以访问

echo $_POST['key1'];
====================== 全本

<script src="../../bin/addons/jquery-1.7.2.js"></script>
<script src="../../bin/addons/jquery.form.js"></script>
<script>
// jQuery Form-plugin 
(function() {
  var value1 = "dynamic_value1";
  var value2 = "dynamic_value2";
  $('.dummyForm1').ajaxForm({
    data:{
      key1: value1,
      key2: value2
  }
  complete: function(xhr) {
      txt = xhr.responseText;
      alert(txt);
  }
}); 
})(); 
</script>

<form class="dummyForm1" action="form-php.php" method="post">
    <input type="submit" value="Hit!" />
</form>
form-php.php

<?
  echo "Key1 value:". $_POST['key1'];
?>

数据属性后缺少逗号

试试这个:

(function () {
    var value1 = "dynamic_value1";
    var value2 = "dynamic_value2";
    $('.dummyForm1').ajaxForm({
        data: {
            key1: value1,
            key2: value2
        }, //You were missing this comma.
        complete: function (xhr) {
            txt = xhr.responseText;
            alert(txt);
        }
    });
})();

数据属性后缺少逗号

试试这个:

(function () {
    var value1 = "dynamic_value1";
    var value2 = "dynamic_value2";
    $('.dummyForm1').ajaxForm({
        data: {
            key1: value1,
            key2: value2
        }, //You were missing this comma.
        complete: function (xhr) {
            txt = xhr.responseText;
            alert(txt);
        }
    });
})();

给定解决方案的问题在于,当调用事件处理程序时,ajaxform将填充变量

var value1 = "dynamic_value1";
/*This will be sent*/
var value2 = "dynamic_value2";
(function () {
    $('.dummyForm1').ajaxForm({
        data: {
            /*On load of event handler the values are set, they are not dynamic anymore*/
            key1: value1,
            key2: value2
        },
        complete: function (xhr) {
            txt = xhr.responseText;
            alert(txt);
        }
    });
})();
/*This will not be sent*/
value2 = "new value";
可以使用函数返回全局变量的当前状态

var value1 = "dynamic_value1";
/*This will not be sent*/
var value2 = "dynamic_value2";
(function () {
    $('.dummyForm1').ajaxForm({
        data: {
            /*On load of event handler the values are set, they are not dynamic anymore*/
            key1: value1,
            key2: function () {
                /*this returns the current state of the global variable*/
                return value2;
            }
        },
        complete: function (xhr) {
            txt = xhr.responseText;
            alert(txt);
        }
    });
})();
/*This will be sent*/
value2 = "new value";

给定解决方案的问题在于,当调用事件处理程序时,ajaxform将填充变量

var value1 = "dynamic_value1";
/*This will be sent*/
var value2 = "dynamic_value2";
(function () {
    $('.dummyForm1').ajaxForm({
        data: {
            /*On load of event handler the values are set, they are not dynamic anymore*/
            key1: value1,
            key2: value2
        },
        complete: function (xhr) {
            txt = xhr.responseText;
            alert(txt);
        }
    });
})();
/*This will not be sent*/
value2 = "new value";
可以使用函数返回全局变量的当前状态

var value1 = "dynamic_value1";
/*This will not be sent*/
var value2 = "dynamic_value2";
(function () {
    $('.dummyForm1').ajaxForm({
        data: {
            /*On load of event handler the values are set, they are not dynamic anymore*/
            key1: value1,
            key2: function () {
                /*this returns the current state of the global variable*/
                return value2;
            }
        },
        complete: function (xhr) {
            txt = xhr.responseText;
            alert(txt);
        }
    });
})();
/*This will be sent*/
value2 = "new value";

所以您只想初始化键名?我需要在AjaxForm调用期间在JSON中传递一个变量值?您现在的代码应该可以正常工作。您面临的问题是什么?无法使用:echo$_POST['key1']获取值;在里面php@Chandu:现在请参阅我的帖子,我添加了完整的脚本…所以您只想初始化键名?我需要在AjaxForm调用期间在JSON中传递一个变量值?您现在的代码应该可以正常工作。您面临的问题是什么?无法使用:echo$_POST['key1']获取值;在里面php@Chandu当前位置请参考我的帖子,我添加了完整的脚本…非常感谢!添加缺少的逗号修复了这个问题我可以通过blob文件以这种方式上传数据吗?非常感谢!添加缺少的逗号修复了我可以通过这种方式传递blob文件来上传数据的问题吗?