如何将jquery函数应用于每个实例

时间:2012-03-27 12:57:40

标签: jquery each

我正在尝试应用.each方法,以便此页面上的每个“响应文本区域”都是单独定位的(我在一个页面上有多个表单提交,我希望禁用提交按钮每一个,直到您在相应的文本区域中输入文本)。但是我不知道放在哪里。我可以链接方法吗?

(这都是在document.ready中)

$('.submit').attr('disabled', 'disabled');
$('.response-text-area').each.keyup(function() {
    if ($('.response-text-area').val() == "") {
        $('.submit').attr('disabled', 'disabled');
    }
    else {
        $('.submit').removeAttr('disabled');
    }
});

3 个答案:

答案 0 :(得分:3)

只需删除.each(无论如何语法都不正确)。该事件将绑定到具有类response-text-area的每个控件。

$('.response-text-area').keyup(function(){
    if($(this).val() == ""){
        // I'd need to see your markup to provide code here. 
        // You need to find the correct submit button in relationship 
        // to the current item. Something similar to this:
        $(this).parent().find('.submit').attr('disabled', 'disabled');
    }
    else{
        $(this).parent().find('.submit').removeAttr('disabled');
    }
})

答案 1 :(得分:0)

您不需要使用它们,因为它也可以使用

$('.response-text-area').bind("keyup", function(){
    if($('.response-text-area').val() == ""){
         $('.submit').attr('disabled','disabled');
    }
    else{
        $('.submit').removeAttr('disabled');
    }
})

答案 2 :(得分:0)

$('.response-text-area').keyup( function() {
    $( ".submit", this.form ).prop( "disabled", !this.value );
});