要在选中复选框时启用按钮,也启用它

时间:2014-05-01 12:11:09

标签: jquery html button checkbox scroll

我只想在复选框为checkedenabled时启用该按钮,但它不起作用,它还取决于上面的textarea滚动。检查check box后,需要向上和向下滚动以启用按钮 这是我的HTML:

<textarea rows="20">
  The Fédération Internationale de Football Association (FIFA, /ˈfiːfə/; English: Federation of International Football Associations) is the international governing body of association football, futsal and beach soccer. Its membership comprises 209 national associations. Its headquarters are in Zurich, Switzerland, and its president is Sepp Blatter. FIFA is responsible for the organisation of football's major international tournaments, notably the World Cup. The need for a single body to oversee association football became apparent at the beginning of the 20th century with the increasing popularity of international fixtures. FIFA was founded in Paris on 21 May 1904; the French name and acronym persist even outside French-speaking countries. The founding members were the national associations of Belgium, Denmark, France, the Netherlands, Spain (represented by Madrid Football Club; the Spanish federation was not created until 1913), Sweden and Switzerland. Also, that same day, the German Association declared its intention of affiliating through a telegram.
</textarea>
<div>
  <input type="checkbox" disabled="disabled" value="I agree all." id="agre"/>I agree all.
</div>
<div>
  <input type="submit" disabled="disabled" value="Submit" />
</div>

这里是jQuery代码

$(document).ready(function() {
  $("textarea").scroll(function(){
    var scrol=$("textarea").scrollTop();

    if (scrol == '373'){
      $("#agre").removeAttr('disabled');

    }
    else
    {
      $("#agre").attr('disabled','disabled');
    }
    var stat;
    $(":checkbox").change(function(){
      stat=$(this).prop('checked');
    });
    var disabl=$(":checkbox").prop('disabled');
    if($(":checkbox").prop('checked') == true && disabl != true){
      $(":submit").removeAttr('disabled');
    }
    else
    {
      $(":submit").attr('disabled','disabled');
    }
  });
});

3 个答案:

答案 0 :(得分:1)

您正在滚动事件处理程序中定义change()处理程序。尝试在document.ready()

中取出它

答案 1 :(得分:1)

首先(您不需要else,因为用户在完成阅读后可能会向上滚动):

if (scrol > 370) {
  $('#agre').attr('disabled', false);
}

然后,如果textarea滚动侦听器在外面:

$('#agre').change(function() {
  $(':submit').attr('disabled', !$('#agre').is(':checked'));
});

这也短得多,做同样的事情。

答案 2 :(得分:1)

使用此

<强> HTML

   <textarea rows="20" id="txtarea">The Fédération Internationale de Football Association (FIFA, /ˈfiːfə/; English: Federation of International Football Associations) is the international governing body of association football, futsal and beach soccer. Its membership comprises 209 national associations. Its headquarters are in Zurich, Switzerland, and its president is Sepp Blatter. FIFA is responsible for the organisation of football's major international tournaments, notably the World Cup. The need for a single body to oversee association football became apparent at the beginning of the 20th century with the increasing popularity of international fixtures. FIFA was founded in Paris on 21 May 1904; the French name and acronym persist even outside French-speaking countries. The founding members were the national associations of Belgium, Denmark, France, the Netherlands, Spain (represented by Madrid Football Club; the Spanish federation was not created until 1913), Sweden and Switzerland. Also, that same day, the German Association declared its intention of affiliating through a telegram.</textarea>
<div><input type="checkbox"  value="I agree all." id="agre"/>I agree all.</div>
<div><input type="submit" id="txt"  value="Submit" /></div>

<强> JQUERY

$("#agre").attr('disabled',true);
 $("#txt").attr('disabled',true);
$("textarea").scroll(function(){

   var scrol=$("textarea").scrollTop();

        if (scrol >= '373'){
            $("#agre").prop('disabled',false);

        }
        else
        {
            $("#agre").attr('disabled',true);
        }
  });
$(":checkbox").change(function(){
    if($(this).is(':checked'))
           {
                $("#txt").attr('disabled',false);
           }
    else
    {
         $("#txt").attr('disabled',true);
    }
        });

<强> JSFIDDEL