Jquery 我怎样才能在这篇ajax文章中添加更多的变量。。。?
我正在制作一个游戏,我需要通过post请求将一些变量发送到排行榜页面 这是我目前的邮政编码:Jquery 我怎样才能在这篇ajax文章中添加更多的变量。。。?,jquery,ajax,Jquery,Ajax,我正在制作一个游戏,我需要通过post请求将一些变量发送到排行榜页面 这是我目前的邮政编码: var toDatabase = confirm('Do you want to save your score to the database?'); if (toDatabase) { $.post("leaderboards.php", {"Level1": "Level1"}, function() { window.location.href="leaderboards.php
var toDatabase = confirm('Do you want to save your score to the database?');
if (toDatabase) {
$.post("leaderboards.php", {"Level1": "Level1"}, function() {
window.location.href="leaderboards.php";
});
} else {
$.post("gameL2.php", {"Level1": "Level1"}, function() {
window.location.href='gameL2.php';
});
}
这个链接到
leadboards.php
文件的顶级代码也是我希望添加更多变量的代码。如何做到这一点?是否添加一对新的花括号?数据通过对象发送,只需添加另一个键/val:
$.post("leaderboards.php", {"Level1": "Level1", "anotherKey": "anotherVal"}, function() {
数据通过对象发送,只需添加另一个键/val:
$.post("leaderboards.php", {"Level1": "Level1", "anotherKey": "anotherVal"}, function() {
您只需向数据对象添加更多变量
if (toDatabase) {
$.post("leaderboards.php", {"Level1": "Level1","variable1":0,"variable2":"value","variable3":546}, function() {
window.location.href="leaderboards.php";
});
依此类推。您只需向数据对象添加更多变量即可
if (toDatabase) {
$.post("leaderboards.php", {"Level1": "Level1","variable1":0,"variable2":"value","variable3":546}, function() {
window.location.href="leaderboards.php";
});
依此类推。第二个参数只是一个对象:
$.post("leaderboards.php", {"Level1": "Level1", "abc": "def", "num": 123}, function() {
仅用逗号分隔,第二个参数只是一个对象:
$.post("leaderboards.php", {"Level1": "Level1", "abc": "def", "num": 123}, function() {
如果您有一个要发送的基本对象,并且在发生的两个不同事件中只添加了两个,那么您可以使用
jQuery.extend
var baseObj = {
Level1: "Level1"
}
if (toDatabase) {
$.extend(baseObj, { complete: true });
}else{
$.extend(baseObj, {doSomething: "yeah" });
}
这就需要将另一个key/val对添加得更远更灵活如果您有一个要发送的基本对象,并且它只在发生的两个不同事件中添加了两个,那么您可以使用
jQuery.extend
var baseObj = {
Level1: "Level1"
}
if (toDatabase) {
$.extend(baseObj, { complete: true });
}else{
$.extend(baseObj, {doSomething: "yeah" });
}
这就需要将另一个key/val对添加得更进一步、更灵活一些。这里的示例可能会有帮助:这里的示例可能会有帮助:
“anotherKey”:“anotherVal”
:)谢谢,正是我想要的:)(在允许的情况下会接受)。“anotherKey”:“anotherVal”
:)谢谢,正是我想要的:)(如果允许,我会接受)。