我試圖防止在特定輸入字段中輸入值爲0,並在輸入0時顯示錯誤div,所以如果您請求引導我,請它會很友善。如何防止通過jquery在特定輸入中輸入0並通過jquery顯示錯誤div?
我想用jquery請.. ..
<input name="invoice_price" class="form-control">
我試圖防止在特定輸入字段中輸入值爲0,並在輸入0時顯示錯誤div,所以如果您請求引導我,請它會很友善。如何防止通過jquery在特定輸入中輸入0並通過jquery顯示錯誤div?
我想用jquery請.. ..
<input name="invoice_price" class="form-control">
HTML:用於以下領域爲
碼 -
<input name="invoice_price" class="form-control">
<div id="div1" style="color:red"></div>
jQuery的: -
$(document).ready(function(){
$("input[name='invoice_price']").on('blur keyup',function(){
if($(this).val()=='0')
{
$("#div1").html('0 value not allowed');
$(this).val('');
}
else
{
$("#div1").html('');
}
});
});
小提琴鏈接: - http://jsfiddle.net/0mat1amf/1/
由於提問者問了另一個問題在評論部分(即用戶只能輸入最多三個位),然後我在這裏加入更多的代碼:
<input name="invoice_price" class="form-control" type="text" maxlength="3">
這是,如果你想堆起來的錯誤信息,你可以用什麼:
$(document).ready(function(){
$("input[name=invoice_price]").change(function(){
if ($(this).val()==0)
$(this).after("<div>Error message</div>");
});
});
每次輸入被改變,是錯誤的時間輸入元素之後它會創建的錯誤消息。相反,你可能希望有預定義的div和只更改喜歡它的內容:
<input name="invoice_price" class="form-control">
<div></div>
$(document).ready(function(){
$("input[name=invoice_price]").change(function(){
if ($(this).val()==0)
$(this).next().html("Error message");
});
});
它真的很棒..感謝它..! – 2014-08-28 11:13:05
如果答案適用於您,並且您什麼都不需要,請將答案標記爲已接受,這樣當您不再需要他們的幫助時(不會再有人會來幫助您)(您將浪費時間):http ://stackoverflow.com/help/someone-answers – Kyborek 2014-08-28 13:01:53
準確地說,他的投入不是由'invoice_price的id'鑑定,其名字'attribute'元素。 – Gabor 2014-08-28 10:30:59
@ Gabor..plz查看最新的答案 – 2014-08-28 10:31:52
@SalmanKhan ...我認爲這就是你想要的.. – 2014-08-28 10:43:27