简体   繁体   English

如何根据同一行中td元素的输入禁用一行按钮?

[英]How to disable button of one row based on input in td-element in same row?

I'm quite new to JQuery/Javascript and am trying to disable a button with class="settle-button" in certain rows that are placed in a dynamically generated table based on the value of a td-element with class="total" it. 我对JQuery / Javascript还是很陌生,并尝试基于class="total"的td元素的值,在放置在动态生成的表中的某些行中禁用class="settle-button"它。

I came up with the following code: 我想出了以下代码:

$(function () {
    $('table tr').each(function () {
        if ($(this).find('.total').val() == 0.00) {
            $(this).find('.settle-button').attr('disabled', 'disabled');
        }
        else {
            $(this).find('.settle-button').removeAttr('disabled');
        }
    });
});

Unfortunately, this code disables ALL buttons if only one happens to be 0.00. 不幸的是,如果只有一个恰好是0.00,则此代码将禁用所有按钮。 How can this code be made to change only the button in the current row? 如何使此代码仅更改当前行中的按钮? And are there other ways it can be improved? 还有其他可以改进的方法吗?

(PS I am unable to reproduce the issue in this Fiddle ) (PS,我无法在此提琴中重现该问题)

your if statement reads wrong. 您的if语句读取错误。 .val() is used to get the value of a form element, input, select etc. use .html() to check to text in the TD. .val()用于获取表单元素的值,输入,选择等。使用.html()检查TD中的文本。

Try this - 尝试这个 -

$(function () {
    $('table tr').each(function () {
        if ($(this).find('.total').html() == 0.00) {
            $(this).find('.settle-button').prop('disabled', true);
        }
        else {
            $(this).find('.settle-button').prop('disabled', false);
        }
    });
});

Here is the update fiddle 这是更新小提琴

Good point made by Karl-Andre - use .prop . Karl-Andre提出的要点是使用.prop

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. 从jQuery 1.6开始,.attr()方法针对尚未设置的属性返回undefined。 To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method. 若要检索和更改DOM属性,例如表单元素的选中,选定或禁用状态,请使用.prop()方法。

.prop() 。支柱()

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

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