Javascript switch 不接受传递的变量
我有两个下拉菜单,在第一个下拉菜单中选择了某些内容后,我使用开关填充第二个下拉菜单。这主要只是一个实验,因为我之前做的 jquery 看起来真的很慢。如果有人有更好、更快的方法来做到这一点,我洗耳恭听。但我仍然想要这个问题的答案,因为它现在让我很恼火。
第一个下拉菜单 onchange="chngprog(this)"
调用此函数
function chngprog(facility)
{
if(facility.id == 'facilityview'){
var select = document.getElementById('programview');
} else {
var select = document.getElementById('program');
}
var testid = facility.value;
switch(testid){
<?php global $__CMS_CONN__;
$sqlqry = "SELECT * FROM facility_db";
$stmt = $__CMS_CONN__->prepare($sqlqry);
$stmt->execute();
$listfacility = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($listfacility as $case)
{
echo "case ".$case['id'].":\n";
echo "select.options = '';";
$boom = explode(";", $case['programs']);
foreach($boom as $program)
{
echo "select.options[select.options.length] = new Option('$program', '$program');\n";
}
echo "break;\n";
}
?>
}
}
php 从数据库创建所有案例,因为有 50 多个案例,可能对速度因素没有帮助。
顶部的 if 语句用于确定它正在查看哪一组下拉列表,因为有 2 组下拉列表执行相同的操作,但目的不同。
问题是如何让开关触发一个案例。我已经设置了警报以查看发生的情况,并且所有值都是正确的,但除非指定数字,否则它永远不会遇到任何情况。如果我输入 switch(20)
它会遇到 case 20 并将选项添加到第二个下拉列表中,就像它应该的那样。
那么为什么 switch 不评估我输入的变量呢?
I have 2 drop downs and I'm using the switch to populate the second after something has been selected in the first. It's mostly just an experiment because the jquery thing i had doing it before seemed really slow. If anyone has a better, faster way of doing this, I'm all ears. But I still want an answer to this because it's just irritating me now.
The first drop downs onchange="chngprog(this)"
calls this function
function chngprog(facility)
{
if(facility.id == 'facilityview'){
var select = document.getElementById('programview');
} else {
var select = document.getElementById('program');
}
var testid = facility.value;
switch(testid){
<?php global $__CMS_CONN__;
$sqlqry = "SELECT * FROM facility_db";
$stmt = $__CMS_CONN__->prepare($sqlqry);
$stmt->execute();
$listfacility = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($listfacility as $case)
{
echo "case ".$case['id'].":\n";
echo "select.options = '';";
$boom = explode(";", $case['programs']);
foreach($boom as $program)
{
echo "select.options[select.options.length] = new Option('$program', '$program');\n";
}
echo "break;\n";
}
?>
}
}
The php creates all the cases from the database, as there are 50+, probably not helping the speed factor.
The if statement at the top is to determine which set of drop downs it's looking at, as there are 2 sets that do the same thing, but for different purposes.
The problem is getting the switch to hit a case. I've put alerts around to see what happens, and all the values are right, but it never hits a case unless is specify the number. If i put switch(20)
it hits case 20 and adds the options to the second drop down just as it should.
So why isn't the switch evaluating the variable I put in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
facility.value 是字符串吗?尝试
Is facility.value a string? Try
您的 testid 可能是一个字符串,而您的 case 语句需要整数。
尝试:
Your testid is probably a string and your case statements are expecting integers.
Try:
您可能需要将值放在 ' 之间,如下所示:
echo "case '" 。 $case['id'] 。 "':\n";
如果不这样做,则必须在与任何值进行比较之前将变量强制转换为 int (我还假设
id
永远不会返回任何内容与数字不同)。You may want to put your values between ', like this:
echo "case '" . $case['id'] . "':\n";
If you don't, you will have to cast the variable to int before comparing with any value (I'm supossing also that
id
will never return anything different than a number).