2017-05-29 45 views
0

我一直在尋找解決方案,但我無法弄清楚我的代碼有什麼問題。每個文本輸入都正確計算我的累加器變量,但我的複選框,和我的下拉菜單不是。嘗試獲取複選框,請選擇選項值。不添加該值來刪除錯誤

直到所有錯誤== 0,但我的最後三個接受(複選框)和兩個下拉(狀態和產品)都沒有被累加器計數,我無法直接提交。在控制檯中,它們是真實的,但也是(作爲一個數組),但它們沒有被計算,因爲我的錯誤信息仍然顯示它們。任何幫助?

<form action="#" method="post" name="order" id="order"> 
<fieldset> 
<legend>Personal Information</legend> 
<ul> 
    <li><label for="firstname">First Name:</label> 
    <input type="text" maxlength="20" id="first"><span class="1"></span></li> 
    <li><label for="lastname">Last Name:</label> 
    <input type="text" maxlength="20" id="last"><span class="2"></span></li> 
    <li><label for="address">Address:</label> 
    <input type="text" maxlength="40" id="address"><span class="3"></span> 
</li> 
<li><label for="city">City:</label> 
<input type="text" maxlength="30" id="city"><span class="4"></span></li> 
<li><label for="state">State:</label> 
<select name="states" id="states"> 
<option value="selectstate">--Select a State--</option> 
<option value="WA">Washington</option> 
<option value="OR">Oregon</option> 
<option value="MT">Montana</option> 
<option value="CA">California</option> 
<option value="ID">Idaho</option> 
</select><span class="5"></span></li> 
<li><label for="product" id="product">Product:</label> 
<select name="products" id="products"> 
<option value="selectproduct">--Select a Product--</option> 
<option value="OR">Watch</option> 
<option value="MT">Hats</option> 
<option value="CA">Sunglasses</option> 
<option value="ID">Shoes</option> 
<option value="ID">Jewelry</option> 
</select><span class="6"></span></li> 
<li><label for="quantity">Quantity:</label> 
<input type="text" maxlength="5" id="quantity"><span class="7"></span></li> 
<li><label for="contact">Can we contact you by phone?</label> 
<input type="radio" name="contactansweryes" value="Yes" checked>Yes 
<input type="radio" name="contactanswerno" value="No">No <span class="8"> 
</span></li> 
<li><label for="terms">Have you read the Terms and Conditions?</label> 
<input type="checkbox" name="termsanswer[]" id="termsanswer" 
value="accept">I 
Accept <span class="9"></span> 
<li><input type="submit" value="Submit"></li> 
</ul> 
</fieldset> 
</form> 

jQuery的

$(document).ready(function(){ 
    // selects the first text input 
    $(':text:first').focus(); 
    // Values of each input 
    firstVal = $('#first:text'); 
    lastVal = $('#last:text'); 
    addressVal = $('#address:text'); 
    cityVal = $('#city:text'); 
    stateVal = $('#states option:selected'); 
    productVal = $('#products option:selected'); 
    quantityVal = $('#quantity:text'); 
    contactVal = $(':radio:checked'); 
    termsVal = $('#termsanswer:checked'); 

    // console.log(stateVal[0], productVal[0]); 

    $('#order').submit(function(evt) { 
    var error = 0; 
    $('#blankfields').text(""); 
    if ($(firstVal).val() == "") { 
     $('#blankfields').append('<li>Please fill out first name!</li>'); 
     error++; 
    } 
    if ($(lastVal).val() == "") { 
     $('#blankfields').append('<li>Please fill out last name!</li>'); 
      error++; 
    } 
    if ($(addressVal).val() == "") { 
     $('#blankfields').append("<li>Please fill out address</li>"); 
      error++; 
    } 
    if ($(cityVal).val() == "") { 
     $('#blankfields').append("<li>Please enter in City</li>"); 
      error++; 
    } 
    if ($(stateVal).text() == '--Select a State--') { 
     $('#blankfields').append("<li>Please select a State</li>"); 
      error++; 
      console.log(stateVal) 
    } 
    if ($(productVal).text() == '--Select a Product--') { 
     $('#blankfields').append("<li>Please select a Product</li>"); 
      error++; 
    } 
    if ($(quantityVal).val() == "") { 
     $('#blankfields').append("<li>Please select a Quantity</li>"); 
      error++; 
    } 
    if ($(contactVal).length == 0) { 
     $('#blankfields').append("<li>Please select Yes or No to Contact</li>"); 
      error++; 
    } 
    if ($(termsVal).length == 0) { 
     $('#blankfields').append("<li>Please check Accept</li>"); 
      error++; 
    } 
    console.log(error); 
    if (error === 0) { 
     return true; 
    } else { 
    return false; 
    } 
    }); // end of submit 
    }); // end document ready 

回答

0

您需要驗證您的無線電設備,選擇複選框中提交的,而不是ready()事件並使用名稱無線電元素分組像

<input type="radio" name="contactanswer" value="Yes" checked>Yes 
<input type="radio" name="contactanswer" value="No">No <span class="8"> 

而且在JS使用like,

contactVal = $('[name="contactanswer"]:checked'); 

片段,

$(document).ready(function() { 
 
    // selects the first text input 
 
    $(':text:first').focus(); 
 
    // Values of each input 
 
    firstVal = $('#first:text'); 
 
    lastVal = $('#last:text'); 
 
    addressVal = $('#address:text'); 
 
    cityVal = $('#city:text'); 
 
    stateVal = $('#states'); 
 
    productVal = $('#products'); 
 
    quantityVal = $('#quantity:text'); 
 

 

 
    // console.log(stateVal[0], productVal[0]); 
 

 
    $('#order').submit(function(evt) { 
 
    contactVal = $('[name="contactanswer"]:checked'); 
 
    termsVal = $('#termsanswer:checked'); 
 
    var error = 0; 
 
    $('#blankfields').text(""); 
 
    if ($(firstVal).val() == "") { 
 
     $('#blankfields').append('<li>Please fill out first name!</li>'); 
 
     error++; 
 
    } 
 
    if ($(lastVal).val() == "") { 
 
     $('#blankfields').append('<li>Please fill out last name!</li>'); 
 
     error++; 
 
    } 
 
    if ($(addressVal).val() == "") { 
 
     $('#blankfields').append("<li>Please fill out address</li>"); 
 
     error++; 
 
    } 
 
    if ($(cityVal).val() == "") { 
 
     $('#blankfields').append("<li>Please enter in City</li>"); 
 
     error++; 
 
    } 
 
    if ($(stateVal).val() == 'selectstate') { 
 
     $('#blankfields').append("<li>Please select a State</li>"); 
 
     error++; 
 
    } 
 
    if ($(productVal).val() == 'selectproduct') { 
 
     $('#blankfields').append("<li>Please select a Product</li>"); 
 
     error++; 
 
    } 
 
    if ($(quantityVal).val() == "") { 
 
     $('#blankfields').append("<li>Please select a Quantity</li>"); 
 
     error++; 
 
    } 
 
    if ($(contactVal).length == 0) { 
 
     $('#blankfields').append("<li>Please select Yes or No to Contact</li>"); 
 
     error++; 
 
    } 
 
    if ($(termsVal).length == 0) { 
 
     $('#blankfields').append("<li>Please check Accept</li>"); 
 
     error++; 
 
    } 
 
    console.log(error); 
 
    if (error === 0) { 
 
     return true; 
 
    } else { 
 
     return false; 
 
    } 
 
    }); // end of submit 
 
}); // end document ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<form action="#" method="post" name="order" id="order"> 
 
    <fieldset> 
 
    <legend>Personal Information</legend> 
 
    <ul> 
 
     <li><label for="firstname">First Name:</label> 
 
     <input type="text" maxlength="20" id="first"><span class="1"></span></li> 
 
     <li><label for="lastname">Last Name:</label> 
 
     <input type="text" maxlength="20" id="last"><span class="2"></span></li> 
 
     <li><label for="address">Address:</label> 
 
     <input type="text" maxlength="40" id="address"><span class="3"></span> 
 
     </li> 
 
     <li><label for="city">City:</label> 
 
     <input type="text" maxlength="30" id="city"><span class="4"></span></li> 
 
     <li><label for="state">State:</label> 
 
     <select name="states" id="states"> 
 
<option value="selectstate">--Select a State--</option> 
 
<option value="WA">Washington</option> 
 
<option value="OR">Oregon</option> 
 
<option value="MT">Montana</option> 
 
<option value="CA">California</option> 
 
<option value="ID">Idaho</option> 
 
</select><span class="5"></span></li> 
 
     <li><label for="product" id="product">Product:</label> 
 
     <select name="products" id="products"> 
 
<option value="selectproduct">--Select a Product--</option> 
 
<option value="OR">Watch</option> 
 
<option value="MT">Hats</option> 
 
<option value="CA">Sunglasses</option> 
 
<option value="ID">Shoes</option> 
 
<option value="ID">Jewelry</option> 
 
</select><span class="6"></span></li> 
 
     <li><label for="quantity">Quantity:</label> 
 
     <input type="text" maxlength="5" id="quantity"><span class="7"></span></li> 
 
     <li><label for="contact">Can we contact you by phone?</label> 
 
     <input type="radio" name="contactanswer" value="Yes" checked>Yes 
 
     <input type="radio" name="contactanswer" value="No">No <span class="8"> 
 
</span></li> 
 
     <li><label for="terms">Have you read the Terms and Conditions?</label> 
 
     <input type="checkbox" name="termsanswer[]" id="termsanswer" value="accept">I Accept <span class="9"></span> 
 
     <li><input type="submit" value="Submit"></li> 
 
    </ul> 
 
    </fieldset> 
 
</form> 
 
<div id="blankfields"></div>

+0

哦,孩子你從通過我的代碼搜索感謝你這麼多的時間救我!我甚至沒有想到這一點! – Allison

+0

歡迎@Allison。:) –

相關問題