2015-10-28 28 views
1

我需要更改將輸入設置爲required="true"required="false"的應用程序。我的問題是當我嘗試獲取所有需要的輸入時,我只是得到它們全部即使是false之一。獲取必填字段並檢查是否爲真

我試了一下:

$('input[required=true') - >得到所有這些

var $input = $('input'); 
$.each($input, function(sKey, sValue){ 
    if($('#' + sValue.id).prop('required') == "true"){ 
     //Gets all even the false one 
    } 

    if($('#' + sValue.id).prop('required')){ 
     //Gets all even the false one 
    } 

    if($('#' + sValue.id).attr('required') == "true"){ 
     //Gets all even the false one 
    } 

    if($('#' + sValue.id).attr('required') == "required"){ 
     //Gets all even the false one 
    } 

    if($('#' + sValue.id).attr('required') == "required"){ 
     //Gets all even the false one 
    } 

}); 

我在做什麼錯?

+1

'的console.log($( 「輸入[需要= '真']」)的長度。);'爲我在Chrome當元件在HTML是硬編碼工作正常。它們是動態生成的嗎? – epascarello

回答

0

請嘗試如下。

var input = $('input'); 

    $.each(input, function (sKey, sValue) { 
     if (sValue.outerHTML.contains('required="true"')) { 
      //Gets all true 
     } 
    }) 
+0

我知道這就是爲什麼它是一個愚蠢的應用程序。我需要編輯它,但不能改變他們的白癡般的解決方案的必填字段。沒有爲什麼要淡化真假? – Sylnois

+0

抱歉誤解現在檢查答案。 – Azim

+0

沒什麼可說的。我只是對開發者感到憤怒。他們甚至使用iFrames,這是一個2015年的Web應用程序。謝謝您的回答。這工作正常,就像'$(「input [required ='true']':)謝謝 – Sylnois

0

試試這個,

$.each($('input[required=true]'), function(sKey, sValue){ 
    console.log(sValue); 
}); 

將讓你輸入有屬性required='true'

小提琴here