将选定的选项值与php变量进行比较

时间:2014-02-27 07:01:23

标签: javascript php jquery

我的php代码中包含选项列表:

<select class="form-control" name="currentDegree" id="currentDegree">
    <option value="Junior High School">Junior High School</option>
    <option value="Senior High School">Senior High School</option>
    <option value="Diploma">Diploma</option>
    <option value="Bachelor Degree">Bachelor Degree</option>
    <option value="Master">Master</option>
    <option value="Doctor">Doctor</option>
</select>

然后JS标记为

<script>
    var currentDegree = $('#currentDegree').val(); // selected option at listbox
    var minimumDegree = '<php? echo $minimumDegree; ?>'; // it come from the admin backend
    var md; // dinamic variable ( condition depend on minimumDegree )

    function CheckDegree(){                                                                      

        if (minimumDegree == 'Junior High School') {
            md = 0;  
        } else if (minimumDegree == 'Senior High School') {
            md = 1; 
        }
        else if (minimumDegree == 'Diploma') {
            md = 2; 
        }
        else if (minimumDegree == 'Bachelor Degree') {
            md = 3; 
        }
        else if (minimumDegree == 'Master') {
            md = 4; 
        }
        else (minimumDegree == 'Doctor') {
            md = 5; 
        } 
    };
    // Compare the currentDegree variable with External variable
    if (parseint(currentDegree) < md) {
        alert(" Current degree is less than Our minimum requirement !");
        return false;
    }                                                               
</script>  

我在提交时使用onclick =“CheckDegree()”但似乎无法正常工作, 有谁知道JS代码中的错误类型

由于

3 个答案:

答案 0 :(得分:3)

尝试使用:

var minimumDegree =  '<?php echo $minimumDegree; ?>';

而不是:

var minimumDegree = '<php? echo $minimumDegree; ?>';

您需要<?php而不是<php?

答案 1 :(得分:0)

您需要在函数

中移动最后一个条件if (parseint(currentDegree) < md) {

检查此代码的底部

function CheckDegree(){                                                                      

    if (minimumDegree == 'Junior High School') {
        md = 0;  
    } else if (minimumDegree == 'Senior High School') {
        md = 1; 
    }
    else if (minimumDegree == 'Diploma') {
        md = 2; 
    }
    else if (minimumDegree == 'Bachelor Degree') {
        md = 3; 
    }
    else if (minimumDegree == 'Master') {
        md = 4; 
    }
    else (minimumDegree == 'Doctor') {
        md = 5; 
    } 

    // Compare the currentDegree variable with External variable
    if (parseint(currentDegree) < md) {
        alert(" Current degree is less than Our minimum requirement !");
        return false;
    }
};

答案 2 :(得分:0)

改变这个:

'<php echo $minimumDegree; ?>'

为此:

<?php echo $minimumDegree; ?>