2014-02-27 159 views
0

有誰知道如何比較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()",但它似乎並沒有做任何事情。

回答

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的用途。例如: -

<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 
    } 
}; 
+0

感謝隊友@scrowler – user3279136

相關問題