简体   繁体   English

jQuery在输入任何值后在文本框中删除“ 0”

[英]Jquery Removing '0' in textbox after any value is entered

Here is my Fiddle 这是我的小提琴

Initially the textbox won't have any value. 最初,文本框没有任何值。

If any value is entered and it was emptied '0' will be added in it. 如果输入任何值并将其清空,则将在其中添加“ 0”。

But the '0' is not removed after entering any value to it. 但是,向其输入任何值后,不会删除“ 0”。

How can i remove the '0' if any value is entered it. 如果输入任何值,如何删除“ 0”。

Here is my Code : 这是我的代码:

$('.qty').keyup(function(){
   if($(this).val() == ""){
   $('.qty').val('0');
    }else if($(this).val() > 0){
   $(this).val().replace(0,'');
   }
});

Try This Code: 尝试以下代码:

$('.qty').keyup(function(){
   if($(this).val() == ""){
   $('.qty').val('0');
   }else if($(this).val() > 0){
   console.log($(this).val());
   $(this).val().replace(0,'');
   }
    if($(this).val()!=0){
        var text = $(this).val();
        alert(text.slice(0,1));
        if(text.slice(0,1)==0)
        {
            $(this).val(text.slice(1,text.length));   
        }
      }
});

Working Demo 工作演示

Instead of $('.qty').val('0'); 代替$('。qty')。val('0');

Add palceholder. 添加palceholder。

 $('.qty').attr('placeholder','0'); 

If placeholder isn't an option, you could parse value to integer: 如果不是placeholder ,则可以将value解析为整数:

$('.qty').keyup(function(){
    var val = parseInt(this.value, 10);
    this.value = val ? val: 0;
});

.replace() returns the modified string, you need to store it back into the value. .replace()返回修改后的字符串,您需要将其存储回值中。 Try this: 尝试这个:

$('.qty').keyup(function () {
    if ($(this).val() == "") {
        $('.qty').val('0');
    } else if ($(this).val() > 0) {
        console.log($(this).val());
        $(this).val(function() {
            return this.value.replace(/^0+/, '');
        });
    }
});

I've also changed the replace() call to use a regular expression, so it only removes zeroes at the beginning of the input. 我还更改了replace()调用以使用正则表达式,因此它仅在输入的开头删除零。 Otherwise, it will change 102 to 12 . 否则,它将更改为10212

fiddle 小提琴

You could achieve this using blur and focus instead of keyup : 您可以使用blurfocus而不是keyup来实现:

$('.qty').blur(function() {
    if (this.value == '') this.value = '0';
}).focus(function() {
    if (this.value == '0') this.value = '';
});

JSFiddle example JSFiddle示例

Try this.. 尝试这个..

<input type="text" name="test" value="0">


$(document).ready(function(){
    var Input = $('input[name=test]');
    var value= Input.val();

    $(Input).focus(function() {
        if($(this).val() == value)
        {
             $(this).val("");
        }
    }).keyup(function(){
        if($(this).val().length == 0)
        {
            $(this).val(value);
        }
    });
})

Try like this, 这样尝试

$('.qty').keyup(function(){
   if($(this).val() == ""){
   $('.qty').val('');
   }else if($(this).val() > 0){
   console.log($(this).val());
   $(this).val().replace(0,'');
   }
});

Instead of replace 0, replace with whole value using javascript Number function 而不是替换0,而是使用javascript Number函数将其替换为整个值

$('.qty').keyup(function(){
   if($(this).val() == ""){
   $('.qty').val('0');
   }else if($(this).val() > 0){
   console.log($(this).val());
   $(this).val(Number($(this).val()))
   }
});

refer jsFiddle 参考jsFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM