如何使用脚本禁用当前行的输入文本值

时间:2015-05-20 09:26:36

标签: javascript jquery html

function bangali() {
    $(document).ready(function() {
        $(".amtcollected").keyup(function() {
            $(".amtcollected1").attr("disabled", true);
        });
        $(".amtcollected1").keyup(function() {
            $(".amtcollected").attr("disabled", true);
        });
    });
}
bangali();

4 个答案:

答案 0 :(得分:2)

您应该使用.prop()

.prop('disabled', true);

此外,您可以像这样简化和重写它:

$(document).ready(function() {
    $('.amtcollected, .amtcollected1').keyup(function(event) {
        $(event.currentTarget).prop('disabled', true);
    });
});

答案 1 :(得分:1)

您不会传递布尔值,而是传递字符串"disabled"

.attr("disabled", "disabled");

答案 2 :(得分:1)

查找行索引
var row_index = $(this).parent('table').index(); 

并设置已禁用

$("table").eq(row_index).disabled = true;

我没有测试它

答案 3 :(得分:0)

jQuery 1.6+使用prop()

$('#id').on('event_name', function() {
    $("input").prop('disabled', true);   //Disable input
    $("input").prop('disabled', false);  //Enable input 
})

jQuery 1.5及以下使用attr()

$('#id').on('event_name', function() {
    $("input").attr('disabled','disabled');   //Disable input
    $("input").removeAttr('disabled');  //Enable input 
})

注意 :请勿使用.removeProp()方法删除已选中,已禁用或已选中的原生属性。这将完全删除属性,一旦删除,就不能再次添加到元素。使用.prop()将这些属性设置为false。