开发者

Javascript switch not taking passed variable

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);
        fo开发者_开发问答reach($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?


Is facility.value a string? Try

var testid = parseInt(facility.value, 10);


Your testid is probably a string and your case statements are expecting integers.

Try:

switch (parseInt(testid)) {
    ...


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜