我有以下 Javascript 函数,其中每个函数都向 URL 添加一个变量,我想将它们组合起来:
<script>
function showDuration(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "Loading Please Wait";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?duration="+str,true);
xmlhttp.send();
}
}
function showDelivery(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "Loading Please Wait";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?delivery="+str,true);
xmlhttp.send();
}
}
这是有问题的,因为两个变量都需要出现在 url 中,否则,我会收到未选择的错误。更准确地说,我想结合:
xmlhttp.open("GET","getuser.php?delivery="+str,true);
和
xmlhttp.open("GET","getuser.php?duration="+str,true);
提前致谢,如有任何帮助,我们将不胜感激。
更新
var duration = null;
var delivery = null;
function setDuration(_duration) {
duration = _duration;
makeRequest();
}
function setDelivery(_delivery) {
delivery = _delivery;
makeRequest();
}
function makeRequest() {
if (duration != null && delivery != null) {
var url =
"getuser.php?duration=" + encodeURIComponent(duration) +
"&delivery=" + encodeURIComponent(delivery);
// do the ajaxy stuff
}
}
function showDuration(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "Loading Please Wait";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?duration="+str,true);
xmlhttp.send();
}
}
function showDelivery(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "Loading Please Wait";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?delivery="+str,true);
xmlhttp.send();
}
}
</script>
更新2:
<script>
var duration = null;
var delivery = null;
function showDuration(_duration) {
duration = _duration;
makeRequest();
}
function showDelivery(_delivery) {
delivery = _delivery;
makeRequest();
}
function makeRequest() {
if (duration != null && delivery != null) {
var url =
"getuser.php?duration=" + encodeURIComponent(duration) +
"&delivery=" + encodeURIComponent(delivery);
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
document.getElementById("txtHint").innerHTML = "<div class='loading-indication'><img src='ajax-loader.gif' /> Please wait... Loading New Courses...</div>";
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET",url);
xmlhttp.send();
}
}
最佳答案
我会这样做:
var duration = null;
var delivery = null;
function setDuration(_duration) {
duration = _duration;
makeRequest();
}
function setDelivery(_delivery) {
delivery = _delivery;
makeRequest();
}
function makeRequest() {
if (duration != null && delivery != null) {
var url =
"getuser.php?duration=" + encodeURIComponent(duration) +
"&delivery=" + encodeURIComponent(delivery);
// do the ajaxy stuff
}
}
或者如果将其封装起来则更好,这样变量就不会暴露。
关于javascript - 通过 JavaScript 传递多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29224546/