1

选择课程

<select>
    <option value="b.a">B.A</option>
    <option value="b.com">B.Com</option>
    <option value="b.b.a">B.B.A</option>
</select> 

选择学期

<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>

现在我想要的是当我选择第一门课程时b.a。那就不要。学期数应仅显示在“选择学期”列表中,对于 b.com,这些应为 2,对于 bba,这些应为 3,即一旦选择课程,页面会自动刷新并且学期数会根据选择的主题,关于这个我如何使用 js 或 jquery 或 ajax 来完成这个,请通过提供一些示例或教程来帮助我。

4

2 回答 2

1

使用 jQuery 和 AJAX 的示例

HTML

<select id="selPrimary">
    <option value="b.a">B.A</option>
    <option value="b.com">B.Com</option>
    <option value="b.b.a">B.B.A</option>
</select>
<select id="selSecondary"></select>

脚本

// jQuery doc.ready function
$(function() { // all "action" script goes in here
    // Selects primary "select tag" by ID and assigns change event
    $("#selPrimary").on("change", function(e) {
        // Empty the options that might exist 
        // (from last choice in primary) and prep 
        // secondary for new options
        $("#selSecondary").empty();
        // .post is jQuery short hand for .ajax(..."type=post"
        $.post("someFolder/someController.php", // this first param tells location of your php backend controller
            function(data) { // this is the function that acts if the post is "successful"
                $("#selSecondary").html(data); //  fills secondary select tag with our new options
            },
            "json"); // this last param tells how to return the data
    });
})

控制器(本例中为 PHP)

$retValue = "";
//Get Post Variables. 
if (isset($_POST['selPrimary'])){
    $value = $_POST['selPrimary'];   
    switch($value) {
        case "b.a":
            $retValue = '<option value='1'>1st</option><option value='2'>2nd</option<option value='3'>3rd</option>';
            break;
        case "whatev":
            $retValue = '<option value='1'>1st</option><option value='2'>2nd</option<option value='3'>3rd</option>';
            break;
    }
}

echo json_encode($retValue); 
于 2012-11-04T20:59:20.997 回答
0

你可以试试这个...

<script>
function changeSem(sub)
{
 if(sub == 'b.a')
 {
  document.getElementById('sem').innerHTML = "<select id='semnum'>
<option value='1'>1st</option>
</select>";
 }
 if(sub == 'b.com')
 {
    document.getElementById('sem').innerHTML = "<select id='semnum'>
<option value='1'>1st</option>
<option value='2'>2nd</option>
</select>";
 }
 if(sub == 'b.b.a')
 {
    document.getElementById('sem').innerHTML = "<select id='semnum'>
<option value='1'>1st</option>
<option value='2'>2nd</option>
<option value='3'>3rd</option>
</select>";
 }
}
</script>
Select Course
<select id="sub" onchange="changeSem(this.value);">
<option value="b.a">B.A</option>
<option value="b.com">B.Com</option>
<option value="b.b.a">B.B.A</option>
</select> 
Select Semester
<div id="sem"></div>

希望能帮助到你!

于 2012-11-04T18:29:35.187 回答