选择类(class)
<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>
现在我想要的是,当我选择第一门类(class)时,即 b.a
。那就不要。学期数应仅显示在“选择学期”列表中,对于 b.com,这些应为 2,对于 b.b.a,这些应为 3,即一旦选择类(class),页面就会自动刷新,并且学期数会根据所选主题而变化,关于这个我如何使用js或jquery或ajax来完成此操作,请通过提供一些示例或教程来帮助我。
最佳答案
使用 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>
Script
// 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
});
})
Controller (PHP in this case)
$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);
关于javascript - 我想使用 javascript 或 jquery 更改内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13221461/