2014-05-04 84 views
0

HTML代碼:輸入[MAX]爲驗證不起作用

<input type='number' name='input_name_here' max='10' placeholder='Enter a number less than 10'> 

$('input[max]').each(function() 
{ 
    maxValue=$(this).attr("max"); 
    inputValue=$(this).val(); 
    if(inputValue>maxValue) 
    alert("Please consider the Maximum permited value"); 
}); 

但是,這是行不通的。我做對了嗎?

+1

你知道你在做什麼嗎? –

+1

你能告訴我們你的html與這個jquery一起去嗎? – Cyassin

+0

感謝html行。你能解釋什麼不起作用嗎?你可以確認它是否進入循環的警報或什麼? – Cyassin

回答

0

有一個在你的邏輯沒有錯誤。

$('input[max]').each(function()對於max屬性的輸入將會正常工作。如果你的代碼沒有輸入each部分,你需要檢查jQuery代碼的其餘部分。

我用你的代碼做了一個Fiddle,看看它。這實際上起作用。對於數學輸入,建議您包含parseIntparseFloat,以省略驗證錯誤。

+0

是的,小提琴符合我的預期。將挖掘到我的JavaScript。謝謝。 –

-1

您使用的數據 - 屬性

使用如下:

$("input").find("[data-max='" + current + "']"); 
0

我還以爲你在做這個

<input type="" max="50" name="max" value="100"> 


$('input[name="max"]').each(function() 
{ 
    var maxValue=$(this).attr("max"); 
    var inputValue=$(this).val(); 
    alert(maxValue);alert(inputValue); 
    if(inputValue>maxValue) 
    { 
     alert("yes"); 
    } 
}); 
+0

如果您使用'input.max'(帶有max類)而不是'input [name =「max」]'會更好。 –