将所选选项值与其他变量进行比较

时间:2014-02-27 03:22:49

标签: javascript php jquery

有谁知道如何比较jQuery中的选定值?

我有一个最低要求变量,假设它是$CD(当前度变量),然后我需要将它与列表框中的选定值进行比较。变量A不能超过其他选定的索引变量。

HTML:

<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 = $('#degreeofCollegeoption').val(); // selected option
    var minimumDegree = 3; // i set it for bachelor degree, actuallyit shuld <'<?php echo varX; ?>'> from admin 
    var CD; // dinamic variable ( with selected condition )

    functionCheckDegree(){                                                                      
        // Declare the var cd with value, so it will easilly compared                                           
        if (currentDegree == 'Junior High School') {
            mp = 0;  
        } else if (currentDegree == 'Senior High School') {
            mp = 1; 
        }
        else if (currentDegree == 'Diploma') {
            mp = 2; 
        }
        else if (currentDegree == 'Bachelor Degree') {
            mp = 3; 
        }
        else if (currentDegree == 'Master') {
            mp = 4; 
        }
        else (currentDegree == 'Doctor') {
            mp = 5; 
        } 
    };
    // Compare the currentDegree variable with External variable
    if (mp < minimumDegree) {
        alert(" Current degree is less than Our minimum requirement !");
    }                                                               
</script>  

我在提交时使用onclick="CheckDegree()",但它似乎没有做任何事情。

1 个答案:

答案 0 :(得分:0)

首先 - 您错过functionCheckDegree之间的空格 - 因此Javascript实际上无法识别该功能。


如果你想在提交时调用该函数,你需要在其中放置一个动作。目前你的函数只是分配一个变量,实际的比较和alert部分在函数之外:

function CheckDegree(){                                                                      
    // Declare the var cd with value, so it will easilly compared                                           
    if (currentDegree == 'Junior High School') {
        mp = 0;  
    } else if (currentDegree == 'Senior High School') {
        mp = 1; 
    } else if (currentDegree == 'Diploma') {
        mp = 2; 
    } else if (currentDegree == 'Bachelor Degree') {
        mp = 3; 
    } else if (currentDegree == 'Master') {
        mp = 4; 
    } else (currentDegree == 'Doctor') {
        mp = 5; 
    } 
    // Compare the currentDegree variable with External variable
    if (mp < minimumDegree) {
        alert(" Current degree is less than Our minimum requirement !");
        return false; // you'll probably want to do this if it's stopping your form action
    } 
};

另外请注意,将mp值作为option元素的值放置并不容易 - 这就是value的用途。 E.g:

<option value="0">Junior High School</option>
<option value="1">Senior High School</option>

然后你可以简化你的功能:

function CheckDegree(){                                                                      
    var currentDegree = $('#currentDegree').val(); // selected option
    var minimumDegree = 3;

    if (parseInt(currentDegree) < minimumDegree) {
        alert(" Current degree is less than Our minimum requirement !");
        return false; // you'll probably want to do this if it's stopping your form action
    }
};