Javascript if语句,以确定在xmlhttp.open GET上调用哪个url

Javascript if语句,以确定在xmlhttp.open GET上调用哪个url,javascript,Javascript,正在寻找一种基于选择来编写if语句的方法,该选择将确定xmlhttp.open(“GET”使用的url。示例1,2,3 selected“test2.php”q=“+str,4 selected”test3.php?q=“+str,5 selected”test4.php?q=“+str <form style="width: 210px"> <select name="bookings" onchange="showbookings(this.value)">

正在寻找一种基于选择来编写if语句的方法,该选择将确定
xmlhttp.open(“GET”
使用的url。示例1,2,3 selected
“test2.php”q=“+str
,4 selected
”test3.php?q=“+str
,5 selected
”test4.php?q=“+str

<form style="width: 210px">
    <select name="bookings" onchange="showbookings(this.value)">
        <option value="">Select Booking Types To See:</option>
        <option value="1">All Booking</option>
        <option value="2">Open Bookings</option>
        <option value="3">Closed Bookings</option>
        <option value="4">Potential Bookings</option>
        <option value="5">Create Bookings</option>
    </select>
</form>
<div id="txtBookings"></div>


function showbookings(str) {
if (str === "") {
    document.getElementById("txtBookings").innerHTML = "";
    return;
}
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("txtBookings").innerHTML = xmlhttp.responseText;

    }
};
xmlhttp.open("GET", "test2.php?q=" + str, true);
xmlhttp.send();
}

选择要查看的预订类型:
所有预定
公开预订
封闭式预订
潜在预订
创建预订
功能展示预订(str){
如果(str==“”){
document.getElementById(“txtBookings”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtBookings”).innerHTML=xmlhttp.responseText;
}
};
open(“GET”、“test2.php?q=“+str,true”);
xmlhttp.send();
}

您可以试试这样的方法

function showbookings(str) {
    if (!str) {
        return;
    }
    var num = str > 3 ? str-1 : 2;
    xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = function () {
        // ...
    }

    xmlhttp.open("GET", "test"+num+".php?q=" + str, true);
    xmlhttp.send();
}

IIya-我很感激这个职位。对我来说很好。谢谢,Kurt
function showbookings(str) {
    if (!str) {
        return;
    }
    var num = str > 3 ? str-1 : 2;
    xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = function () {
        // ...
    }

    xmlhttp.open("GET", "test"+num+".php?q=" + str, true);
    xmlhttp.send();
}