最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

if statement - jQuery checkbox to enabledisable text input and addremove default value - Stack Overflow

programmeradmin0浏览0评论

I am trying to create a small reusable function that will, when checked, disable a text field and insert a default value of '160', and when unchecked, enable the field and remove the value. I have it mostly finished but the unchecking part is throwing me off.

$('#chkIsTeamLead').change(function(){


   if ($('#chkIsTeamLead').checked = true){
      $('#txtNumHours').val('160').attr('disabled', 'disabled');
      console.log('checked');
   }

  if ($('#chkIsTeamLead').checked = false){
     $('#txtNumHours').val('').removeAttr('disabled');
     console.log('unchecked');
   }

});

I had it setup as a reusable function with arguments passed to it but that was giving me more trouble, I would like the arguments to be checkbox, target, and value preferably link to my current code: Thanks for any help you can provide!

I am trying to create a small reusable function that will, when checked, disable a text field and insert a default value of '160', and when unchecked, enable the field and remove the value. I have it mostly finished but the unchecking part is throwing me off.

$('#chkIsTeamLead').change(function(){


   if ($('#chkIsTeamLead').checked = true){
      $('#txtNumHours').val('160').attr('disabled', 'disabled');
      console.log('checked');
   }

  if ($('#chkIsTeamLead').checked = false){
     $('#txtNumHours').val('').removeAttr('disabled');
     console.log('unchecked');
   }

});

I had it setup as a reusable function with arguments passed to it but that was giving me more trouble, I would like the arguments to be checkbox, target, and value preferably link to my current code: http://codepen.io/AlexBezuska/pen/bwgsA Thanks for any help you can provide!

Share Improve this question asked May 29, 2013 at 6:21 glassesglasses 7632 gold badges8 silver badges24 bronze badges 5
  • 1 Use $('#chkIsTeamLead').is(':checked') in your conditions. – user1823761 Commented May 29, 2013 at 6:23
  • And you must compare two values with ==. – user1823761 Commented May 29, 2013 at 6:24
  • 3 And you can use if(){} else{} rather than 2 if's – nix Commented May 29, 2013 at 6:25
  • Check this jsFiddle. – user1823761 Commented May 29, 2013 at 6:25
  • I had it setup as a reusable function with arguments passed to it but that was giving me more trouble What trouble ? Can you post that code where you tried to pass arguments as well ? – SachinGutte Commented May 29, 2013 at 6:30
Add a comment  | 

2 Answers 2

Reset to default 14

Working jsFiddle Demo

  1. Use .is(':checked').
  2. You must compare two values with ==.
  3. When you are working with attribute like disabled, it's better to use .prop() method instead of .attr().

$('#chkIsTeamLead').change(function(){
    if ($('#chkIsTeamLead').is(':checked') == true){
        $('#txtNumHours').val('160').prop('disabled', true);
        console.log('checked');
    } else {
        $('#txtNumHours').val('').prop('disabled', false);
        console.log('unchecked');
    }
});

References:

  • .attr() - jQuery API Documentation
  • .prop() - jQuery API Documentation
  • .is() - jQuery API Documentation

try this

$('#chkIsTeamLead').change(function(){


   if ($('#chkIsTeamLead').is(':checked')){
      $('#txtNumHours').val('160').attr('disabled', 'disabled');
      console.log('checked');
   } else {
     $('#txtNumHours').val('');
    $('#txtNumHours').removeAttr('disabled');
     console.log('unchecked');
   }
 });
发布评论

评论列表(0)

  1. 暂无评论