简体   繁体   English

当值为0时,更改复选框-仅输入数字

[英]When value is 0, change checkbox - input only accept digits

I have a checkbox that when clicked has an input value of 1. When it is unchecked the value is 0. That is ok. 我有一个复选框,单击该复选框时,其输入值为1。如果未选中,则值为0。可以。

The input only accepts digits, the validation is ok. 输入仅接受数字,验证正常。 I want it to work in a way that when the value is 0 the checkbox is not checked. 我希望它以某种方式工作,即当该值为0时,该复选框未选中。

My example sometimes fails when I play with values of one digit, or more than two. 当我使用一个数字或两个以上的值时,我的示例有时会失败。

  jQuery(document).on('keypress keyup blur', 'input.e-qty', function (event) {
        if ((((event.which > 47) && (event.which < 58)) || (event.which == 13)) ) {
            var qty = jQuery(this).val();
            if(qty.length == 1 && qty == 0){
                jQuery('.product').trigger('click');
            }

            return true;
        }
        return false;        
    });


    jQuery(document).on("change", ".product", function () {
        var id = this.id.split("_").pop();
        var qty = jQuery('#qty_' + id);

        qty.val(+this.checked);

    });

https://jsbin.com/patekocuya/1 https://jsbin.com/patekocuya/1

Additionally you can use the attr property, since attributes could be altered at runtime also, therefore your code would look like this.. 另外,您可以使用attr属性,因为属性也可以在运行时更改,因此您的代码将如下所示。

jQuery(document).on('keypress keyup blur', 'input.e-qty', function (event) {
        if ((((event.which > 47) && (event.which < 58)) || (event.which == 13)) ) {
            var qty = Number(jQuery(this).val());

            if(qty == 0){

                $('.product').attr('checked', true);

            }else{

                $('.product').attr('checked', false);
            }

            return true;
        }
        return false;        
    });

Note 注意

Also take note that checking if you are working with classes it is best to tell the page which checkbox you are referring to since class referencing returns a collection. 另请注意,检查类是否在使用时,最好告诉页面所引用的复选框,因为类引用会返回一个集合。 using this method you could modify the HTMLInputElement s property directly, 使用此方法,您可以直接修改HTMLInputElement的属性,

therefore your code becomes - 因此您的代码变为-

 $('.product')[0].checked = true;
 or
 $('.product')[0].checked = false; 

Use prop() function instead of click 使用prop()函数而不是单击

  1. Add else statement with not equal 0 with checked true 添加else语句,其值为0且不等于true
  2. Just use parseFloat for the numeric problem 只需将parseFloat用于数值问题

Updated 更新

https://jsbin.com/coqutuzizo/1/edit?html,js,console,output https://jsbin.com/coqutuzizo/1/edit?html,js,console,output

 jQuery(document).on('keypress keyup keydown blur', 'input.e-qty', function (event) {
    if ((((event.which > 47) && (event.which < 58)) || (event.which == 13) || (event.which == 8)) ) {
        var qty = parseFloat(jQuery(this).val());
        if(qty === 0 || !$.trim($(this).val())){
            jQuery('.product').prop('checked',false);
        }else{
            jQuery('.product').prop('checked',true);

        }

        return true;
    }
    return false;        
});

Is this what you are looking for https://jsfiddle.net/qwy7ausj/ 这是您正在寻找的 https://jsfiddle.net/qwy7ausj/

jQuery(document).on('keypress keyup blur', 'input.e-qty', function(event) {

var qty = jQuery(this).val();
if (qty != 0) {

    jQuery('.product').prop('checked', true);
} else {
    jQuery('.product').prop('checked', false);
}

});

You should seperate keypress and keyup otherwise you will get a double value. 你应该独立keypresskeyup否则你将得到双重价值。

Here i made you an example. 在这里,我举了一个例子。

 jQuery(document).on('keypress', 'input.e-qty', function (event) { if ((((event.which > 47) && (event.which < 58)) || (event.which == 13)) ) return true; return false; }); jQuery(document).on('keyup', 'input.e-qty', function (event) { var qty = $(this).val(); if(qty != "" ){ var i = parseInt(qty); jQuery('.product').prop("checked", i>0).trigger("change") } }); jQuery(document).on("change", ".product", function () { var id = this.id.split("_").pop(); jQuery('#qty_' + id).val(+this.checked).focus().select(); ; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" id="chk_1" class="product"> <input type="text" class="e-qty" name="qty" id="qty_1" value="0"> 

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

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