簡體   English   中英

jQuery-檢查輸入文本的值

[英]Jquery - check the value of inputs texts

我有一些這樣的輸入文本:

<input type="text" value="5" maxlength="12" id="qty" class="input-text qty" name="qty2050" tabindex="1">

and 

<input type="text" value="0" maxlength="12" id="qty" class="input-text qty" name="qty2042" tabindex="1">

我想用jquery檢查每個輸入的值,以執行一個函數,如果一個輸入中的數量不同於0。

編輯在撤離之前,我該怎么做在頁面上?

感謝幫助。

以這種方式嘗試-

$(window).unload(function() {
     $('.input-text qty').each(function (){
         var val = parseInt($(this).val(),10);
         // you can use math.floor if your input value is float-
         //var val = Math.floor($(this).val());
         if(val !== 0)
         alert(val);
    });
});   

頁面上不應有兩個相同的ID。 非常簡單的應該是:

$('input.input-text.qty').each(function(){
  if ($(this).val() !== '0') {
    alert('Gotcha!');
    return false; //quit loop since we're ok with 1 occurance
  }
});
$('.input-text').change( function() { 
    if($(this.val() != "0")){ // 
      action here
    }
});

使用change(): http//api.jquery.com/change/

示例:向所有文本輸入元素添加有效性測試:

$("input[type='text']").change( function() {
    // check input ($(this).val()) for validity here
});
var checkInput = function() {
    if(this.value !== '0') {
        // do something
    }
}

$(window).bind('unload', checkInput);
$('input[type=text]').change(checkInput);

我希望這有幫助。

如果您確實希望在卸載事件上執行此操作,則類似這樣的事情將起作用(未經測試),否則綁定到您想要的任何事件:

$(window).unload( function() {
    $('.input-text qty').each(function (){
        if( $(this).val() !== '0' ) {       
            alert("there is a 0");
            return false;//breaks out of the each loop, if this line present will only get one alert even if both fields contain 0
        }
    });
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM