2016-10-24 18 views
0

我在我的網頁中放置了代碼爲'n'的行中包含下拉列表的每個行中的代碼。有沒有一種方法可以在所有下拉列表都包含一個值的情況下啓用提交按鈕(即)即使一個下拉列表爲空也不能啓用該按鈕。另外,我希望提交按鈕在第一次點擊後被禁用。如何僅在while循環中的所有下拉列表都不爲空時啓用提交按鈕

我試過下面的代碼,但不起作用。

$rowIndex = 0; 
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
<form> 
    <div> 
     <select name = error_type id='error_type$rowIndex' class='picker'> 
      <option disabled selected value> -- select an option -- </option> 
      <option value=2_Stage_Error>2_Stage_Error</option> 
      <option value=BG_Error>BG_Error</option> 
     </select> 
    </div> 
    $rowIndex++; 
    } 
    <input type='submit' value='SUBMIT' id='submit' onclick=report_submit()disabled 
      = true /> 
</form> 

的select語句,它指定要求用戶提交表單前選擇一個值JS

<script type= "text/javascript"> 
$(function() { 
    $('.picker').on('change', function() { 
    var $sels = $('.picker option:selected[value=""]'); 
    $("#submit").attr("disabled", $sels.length > 0); 
    }).change(); 
}); 
</script> 
+1

首先,使用'jQuery.prop',而不是'attr' – Rayon

+0

https://jsfiddle.net/rayon_1990/dvphbf5f/1/ – Rayon

+0

你故意把while循環外表格? – Beginner

回答

0

首先使按鈕禁用。我的意思是提交按鈕。 那就試試這個:

function checkAllHasValue(){ 
    $(".picker").each(function(){ 
    var elem = $(this); 
    if(elem.val()+"" == "undefined"){ 
    return false; 
    } 

}) 
    return true; 
} 

$(".picker").on("change",function(){ 
    if(checkAllHasValue){ 
    $("submitbutton").removeAttr("disabled"); 
    } 

}) 
相關問題