你最好将其分成步骤,这是(我认为)理解它的最简单的方法。
我不确定如何将其包含在您的 PHP 中,因为我对您的代码的其余部分一无所知。我会给你一个静态的例子,你必须自己将它与 PHP 结合起来。我希望它无论如何都会有帮助!
第一步:引入jquery.
您必须在 html 之间包含 jQuery 库<head>
像这样的标签:
(如果您已经包含 jQuery,请跳过此步骤)
<!--including jQuery here -->
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
第 2 步:使用 JavaScript 工作。您首先必须触发 onload 回调,以便您的函数知道何时关闭,如下所示:
<script>
//When page is done loading
$(document).ready(function(){
});
</script>
第三步:大脚本
//When page is done loading
$(document).ready(function() {
// let's get your select first!
var firstSelector = $('#yourSelectID');
var medicSelector = $('#yourMedicSelectID');
//we will fill this variable on select changes
var currentSelection = firstSelector.val();
//set initial medic values
medicSelector.html(
"your options here as strings (between the quotes). see below for example"
);
//get value on change
firstSelector.change(function() {
currentSelection = $(this).val();
//check if the current value is what you want
if (currentSelection == "1") {
//This replaces the options completely
medicSelector.html(
//you can use any PHP variable here to fill it.
//Just use <?php echo $optionsforthischoice ?>
// $optionsforthischoice would be filled like so:
// $optionsforthischoice = "<option>option1</option><option>option2</option> etc.;
"<option>option1</option><option>option2</option><option>option3</option>"
);
}
if (currentSelection == "2") {
// do the same here as for 1, but when 2 is selected!
}
});
});
重要的提示:
正如您所看到的,这种风格非常静态。如果您想要一个更动态的解决方案,您将为我们提供更好的信息和更多代码。
此 Javascript 替换了您之前的选项,因此您必须为每个 if 语句重写它们。他们不做加法或减法。他们完全取代了。
!EDIT!
我看到你已经编辑了你的问题。现在我可以适当地提供帮助。我将上面的“教程”留下来供参考。 (顺便说一句,您也可以使用 php foreach 循环将完整数组放入您的 JavaScript 中)
见下文
如果您想正确执行此操作,您可能需要连接数据库中的点,并使用 sql 连接。它会为您节省大量代码并使整个事情变得更容易。但是,由于您可能还没有这样做,因此只有一种正确的方法可以解决此问题。
您会发现,您不能在特定功能上使用 ajax。你必须制作一个 php 脚本。
例如,您将制作“getMedics.php”。
然后,当选择更改时,您将使用 ajax 调用“getMedics.php”文件,如下所示:
//The first option is the link to the file
//The ?specialty is a parameter which you are going to get in the script.
//This will be the selection that was made before
//Within the function "result" will be the variable with the values the phpscript returns
$.get('phpscripts/getMedics.php?specialty=' + $("#specialties").val(), function(result) {
//if the result returned anything
if (result.trim()) {
//you will want the result to be the options
$('#medics').html(result);
}
});
现在 php 脚本会变得更加棘手。这是关于它的外观:
//get the specialty ID
$specialty = strip_tags($_GET['specialty']);
//get the medics
//i'm guessing the results will be arrays within arrays
$medics = Medic::getMedics();
//set the array for the options
$options = array();
//prepare our results
$optionResults = "";
switch ($specialty) {
case 0:
$options[1] = "1";
$options[2] = "2";
$options[3] = "5";
$options[4] = "7";
break;
case 1:
$options[1] = "1";
$options[2] = "2";
$options[3] = "5";
$options[4] = "7";
break;
case 2:
echo "specialty equals 2";
break;
}
//this foreach loop will pass the options to a variable
foreach($option as $options){
//.= adds to the variable
$optionResults .= "<option value='".$medics[$option][0]."'>".$medics[$option][1]."</option>";
}
//this is what will return as the ajaxed results:
echo $optionResults;