【发布时间】:2015-11-27 18:08:06
【问题描述】:
我有 2 个 js 文件,我想将 2 个变量从 file1.js 传递给 file2.js。到目前为止,我所做的是将这两个变量从 file1.js 发送到带有 ajax 的第三个文件 file3.php 中:
$('#replace').click(function(){
var replacement = cur;
var replacement2 = cur2;
$.ajax({
url: DOMAIN_NAME+"file3.php",
type: "post",
data: {replacement: replacement, replacement2 : replacement2},
success:function(data){
console.log(data);
},
error:function(){
alert('Something Went Wrong');
},
});
});
在我的 file3.php 中:
if(isset($_POST['replacement']) && isset($_POST['replacement2']){
$a = $_POST['replacement'];
$b = $_POST['replacement2'];
}
<input type="hidden" id="af" value="<?=$a;?>">
<input type="hidden" id="bf" value="<?=$b;?>">
在我的 File2.js 中:
var a = $('#af').val();
var b = $('#bf').val();
我可以看到,在网络中,ajax 传递了状态为 200 OK 的变量,但在 php 文件中我的变量没有传递。所以 file2.js 无法获取值。我做错了什么??
【问题讨论】:
-
我认为这个问题对我没有帮助:/
-
你也可以试试jquery cookie github.com/js-cookie/js-cookie