简体   繁体   English

在HTML表格中的复选框的勾号上禁用/启用输入框类型“数字”

[英]Disable/enable input box type 'number' on tick of checkbox in an HTML table

I want to disable/enable input tag with the type number when a checkbox is ticked. 勾选复选框后,我想使用类型编号禁用/启用输入标签。 The checkboxes are on the same row as where the input tag is placed. 复选框与输入标签放置在同一行。 I have tried this method 我尝试过这种方法

$(document).on('change', '.ch_attend', function () {
                        if(this.checked){
                            $(this).closest('tr').find('input:text').prop('disabled', false);
                            $(this).closest('tr').find('button').prop('disabled', false);
                            $(this).closest('tr').find('input:text').val(0);
                            computeKPI();
                        }else{
                            $(this).closest('tr').find('input:text').prop('disabled', true);
                            $(this).closest('tr').find('button').prop('disabled', true);
                            $(this).closest('tr').find('input:text').val(0);
                            computeKPI();
                            $('#chkall').prop('checked', false).uniform(); 
                        }
                    }).find('.ch_attend').change();

and change the input:text to input:number but it didn't work. 并将input:text更改为input:number,但是没有用。 It only works when the type is text 仅当类型为文本时才有效

The type of input on the DOM is number already so it is not input type='text' anymore. DOM上的输入类型已经是数字,因此不再是输入type ='text'

if($retval->num_rows>0){
        while($row = $retval->fetch_assoc()){
            $DOM_string .= "<tr>
                    <td style='text-align-last: center;'><label><input type='checkbox' class='ch_attend'></label></td>
                    <td style='text-align-last: center;'><label>".$row['full_name']."</label></td>
                    <td style='text-align-last: center;'><input disabled='true' style='text-align-last: center; width: 40px; height: 25px;' class='form-control score' data-id='".$row['id']."' type='number' min='0' step='any' value='0'></td>
                    <td style='text-align-last: center;'><input disabled='true' style='text-align-last: center; width: 40px; height: 25px;' class='form-control totalscore' data-id='".$row['id']."' type='number' min='0' step='any' value='0'></td>
                    <td style='text-align-last: center;'><button type='button' disabled='true' class='btn btn-warning btn-xs action' data-full_name='".$row['full_name']."' style='height: 20px; width: 35px; padding-top: 0px; padding-bottom: 0px;'><span class='icon icon-check' style='font-size:10px; line-height: 20px;'></span></button></td>
                </tr>";
            $_SESSION['cnt'] += 1;
        }
    }

HTML markup came from an ajax success result. HTML标记来自ajax成功结果。

I'm confused now. 我现在很困惑。 Please help me on how to do that. 请帮助我。 Thank you. 谢谢。

我认为您正在寻找的是input[type=number]input[type=text]

I tried to find the class instead of find the input type. 我试图找到类而不是找到输入类型。

$(this).closest('tr').find('.score').prop('disabled', false);
$(this).closest('tr').find('.totalscore').prop('disabled', false);

and this one works for me. 这对我有用。 I am wondering if there is another way to do this. 我想知道是否还有另一种方法可以做到这一点。 A shorter code maybe. 可能是较短的代码。

I made a plunker of what you are looking for with raw javascript. 我用原始javascript制作了您想要的东西。

https://plnkr.co/edit/kPF5Mdkh1IeAU0AoW31Y?p=preview https://plnkr.co/edit/kPF5Mdkh1IeAU0AoW31Y?p=preview

The main thing you want to do, after getting the input field with javascript, is to set it's disabled state to true. 在使用javascript获取输入字段之后,您要做的主要事情是将其禁用状态设置为true。

var numberInput = document.querySelector('input[type="number"]');
numberInput.disabled = true;

You can make an function to watch the checkbox change event. 您可以使函数监视复选框更改事件。

<input type="checkbox" onchange="toggleNumberInput(event)">

In this method, you have access to the checked state. 在这种方法中,您可以访问选中状态。

event.target.checked; //will be true when checked, false otherwise.

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

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