2014-10-20 85 views
0

我有一個隱藏輸入的簡單表單,我試圖檢查每個隱藏的輸入是否有一個值。如果所有隱藏的輸入都有一個值,而不是禁用或啓用提交按鈕。一旦用戶通過jquery點擊圖像,輸入就會被填充。香港專業教育學院嘗試多種方式,它看起來像我想的東西....禁用提交按鈕,直到所有隱藏的輸入有一個值

<form method="post" action="test.php"> 
    <div class="selections" id="accordion"> 
     <h3>title<div class='status'>Pending</div></h3> 
     <div class='select-form'> 
      <div class='images'> 
       <img src='images/vote.jpg' data-value='data-value'> 
       <br/><span>title</span><br/>description 
      </div> 
      <input type='hidden' class='image-value' name='1' value=''> 
     </div> 
     <div class='select-form'> 
      <div class='images'> 
       <img src='images/vote.jpg' data-value='data-value2'> 
       <br/><span>title</span><br/>description 
      </div> 
      <input type='hidden' class='image-value2' name='2' value=''> 
     </div> 
    </div> 
    <input id="submit_button" type="submit" class="submit" value="SUBMIT"> 
</form> 

的JavaScript去如下:

$(document).ready(function() { 
    var $submit = $("input[type=submit]"), 
     $inputs = $('input[type=hidden]'); 

    function checkEmpty() { 

     // filter over the empty inputs 

     return $inputs.filter(function() { 
      return !$.trim(this.value); 
     }).length === 0; 
    } 

    $inputs.on('blur', function() { 
     $submit.prop("disabled", !checkEmpty()); 
    }).blur(); // trigger an initial blur 


}); 

什麼想法?

+0

嘗試調試過濾器的長度和檢查什麼它給你 – 2014-10-20 18:33:33

+2

此外,我認爲最終的HTML將我們比PHP更好 – 2014-10-20 18:34:07

+0

由於您使用的是PHP,在循環遍歷類別後設置禁用提交會比在隱藏字段上使用jquery blur更加容易,這種方式無法工作。同樣在你的PHP中,你沒有爲任何隱藏的輸入設置值。如果它們總是空的,它將如何工作? – 2014-10-20 18:37:26

回答

0

你可以只調用checkEmpty()img.click(),並從該函數處理禁用狀態。

嘗試一下這裏:JSFiddle(點擊圖片)

$(document).ready(function() { 
    var $submit = $("input[type=submit]"), 
     $inputs = $('input[type=hidden]'); 

    function checkEmpty() { 
     var res = true; 
     $inputs.each(function(i,v){ 
      if(v.value == ""){ 
       res = false; 
       return false; 
      } 
     }); 

     $submit.prop("disabled", !res);  
    } 

    $("img").click(function(){ 
     $(this).parent().parent().find("input[type=hidden]").val("sdf"); 
     checkEmpty(); 
    }); 

    checkEmpty(); //set disabled onload 
}); 
+0

工作!謝謝! – lancey 2014-10-20 19:33:56

0

,你接受你的裝飾功能不正常,請刪除它像here :

function checkEmpty() { 
    // filter over the empty inputs 
    return inputs.filter(function() { 
     return !(this.value); 
    }).length === 0; 
} 
0

將這個模糊函數內,以找出是否所有隱藏的投入有一個非空值。

var empty=false; 
$('input[type=hidden]').each(function(){ 
if($(this).val()==""){ 
empty=true; 
} 
}); 
if(empty){ 
//DISABLE SUBMIT 
} 
-1

當您不應該使用JavaScript變量時,您正在使用$。工作小提琴:http://jsfiddle.net/keliix06/2f3cv2pk/

$(document).ready(function() { 
    var submit = $("input[type=submit]"), 
     inputs = $('input[type=hidden]'); 

    function checkEmpty() { 

     // filter over the empty inputs 

     return inputs.filter(function() { 
      return !$.trim(this.value); 
     }).length === 0; 
    } 

    inputs.on('blur', function() { 
     submit.prop("disabled", !checkEmpty()); 
    }).blur(); // trigger an initial blur 


}); 
+0

允許JavaScript變量以$開頭,通常用於區分存儲的jQuery對象和其他變量。 – 2014-10-20 19:14:24

相關問題