-1

寻找一种基于选择来编写 if 语句的方法,该选择将确定xmlhttp.open("GET"使用的 url。示例 1,2,3 选中"test2.php?q=" + str, 4 选中"test3.php?q=" + str, 5 选中"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();
}
4

2 回答 2

0
var url = '';
if (str == '5')
    url = 'test4.php?q='+str;
else if (str == '4')
    url = 'test3.php?q='+str;
else if (str != '')
    url = 'test2.php?q='+str;
if (url) {
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
于 2013-10-22T21:11:20.347 回答
0

你可以试试这样的

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();
}
于 2013-10-22T21:22:50.223 回答