簡體   English   中英

簡單的jQuery驗證失敗

[英]simple jQuery validation failed

我想驗證自己的價值,但是如果有人可以幫助我,我就不會做

function countrySelectChanged() {
    $('.sub-menu').hide();
    var selectedCountry = $('#country-select').val();
    if (selectedCountry) {
        $('#' + selectedCountry + '-select').show();
    }
}
$(document).ready(function() {     
    $('#country-select').change(countrySelectChanged);
    countrySelectChanged();

    var valid;

    $('#click').click(function(){           
        if ($("#us-select").val() =="" || $("#germany-select").val() ==""  ){
            valid = false;
        } else {
            valid = true; 
        }

        if (!(valid)) {
            $('#msg').html('NOt Passed')
        } else {
            $('#msg').html('Pass')
        }
    });    
});

這是HTML

<select id="country-select">
    <option value="none" >---</option>
    <option value="us" >US</option>
    <option value="germany">Germany</option>
</select>

<select id="us-select" class="sub-menu hide">
    <option value="none" >---</option>
    <option value="austin"  >Austin</option>
    <option value="austin2"  >Austin2</option>
</select>

<select id="germany-select" class="sub-menu hide">
    <option value="none" >---</option>
    <option value="berlin">Berlin</option>
    <option value="berlin2">Berlin2</option>
</select>

<select id="none-select" class="sub-menu hide">
    <option value="none" >---</option>
</select>

<a href="#" id="click">click me</a>
<div id="msg"></div>​

這是在線版本

http://jsfiddle.net/RPWPZ/14/

工作演示 http://jsfiddle.net/SJ7Sg/ http://jsfiddle.net/95zXa/

要獲得選擇的選項,您需要執行此$("#us-select option:selected").val()

API => http://api.jquery.com/selected-selector/

休息它應該適合原因:)

function countrySelectChanged() {
    $('.sub-menu').hide();
    var selectedCountry = $('#country-select').val();
    if (selectedCountry) {
        $('#' + selectedCountry + '-select').show();
    }
}
$(document).ready(function() {

    $('#country-select').change(countrySelectChanged);
       countrySelectChanged();

    var valid;

    $('#click').click(function(){

   if ($("#us-select option:selected").val() =="none"){

      valid = false;
            }else{
      valid = true;

   }


 if (valid) {
      $('#msg').html('Success')
       return false;
  } else {

      $('#msg').html('Fail')
  }


});


});






​
if ($('#country-select').val() =="us") {
  valid = false;
} else

您必須更改$("#us-select").val() =="" || $("#germany-select").val() =="" $("#us-select").val() =="" || $("#germany-select").val() ==""$("#us-select").val() =="none" || $("#germany-select").val() =="none" $("#us-select").val() =="none" || $("#germany-select").val() =="none"

因為您的select的默認值為“ none”->

<select id="country-select"> <option **value="none"** >---</option> <option value="us" >US</option> <option value="germany">Germany</option> </select>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM