【问题标题】:Get checked unchecked value in jquery在jquery中获取检查未检查的值
【发布时间】:2021-09-27 00:08:29
【问题描述】:

代码仅在一个文本框上显示所需的消息,但我想在每个复选框上显示所需的消息。

它提交表单而不检查所需的验证,只有第一个选中的文本框显示验证,当我选中该框时它没有删除验证。它仍然显示验证消息。

有什么解决办法吗?提前致谢

<div class="row" id="addform_allowances"></div>
$('#add_button').click(function() {
  $('#hrm_employeeDesignation').html("");
  
  //designations dropdown 
  $.ajax({
    url: 'api/Hrm/hrm_payscales_api.php',
    type: 'POST',
    data: {
      action: 'designations'
    },
    success: function(response) {
      $('#hrm_employeeDesignation').append($('<option disabled selected>Choose Option</option>'));
      for (var i = 0; i < response.length; i++) {
        $('#hrm_employeeDesignation').append($('<option>', {
          value: response[i].ID,
          text: response[i].NAME
        }));
      }
    },
    error: function(e) {
      console.log(e);
      alert("Contact IT Department");
    }
  });
  
  //allowances dropdown
  $.ajax({
    url: 'api/Hrm/hrm_payscales_api.php',
    type: 'POST',
    data: {
      action: 'activeallowances'
    },
    success: function(response) {
      for (var i = 0; i < response.length; i++) {
        console.log(response[i].ALLOWANCE);
        var append_allowances = "<div class='col-md-6 form-group'><input name='allowanceid[]' id='checkbox" + i + "'
        type = 'checkbox'
        value = '"+response[i].ID+"' > < label
        for = '' > & nbsp; & nbsp;
        "+response[i].ALLOWANCE+": < /label><input
        id = 'amountcheckbox"+i+"'
        type = 'number'
        name = 'allowance_amount[]'
        class = 'allowance_amount'
        disabled > < /div>";
        $('#addform_allowances').append(append_allowances);
      }
    },

    error: function(e) {
      console.log(e);
      alert("Contact IT Department");
    }
  });
});

$('#addform_allowances').on('click', 'input[type=checkbox]', function() {
  var checkboxidattr = $(this).attr("id");
  if ($('input[type=checkbox][id=' + checkboxidattr + ']').is(':checked')) {
    $('#amount' + checkboxidattr + '').attr('required', 'required');
    $('#amount' + checkboxidattr + '').removeAttr('disabled');
  } else {
    $('#amount' + checkboxidattr + '').removeAttr('required');
    $('#amount' + checkboxidattr + '').attr('disabled');
    $('#amount' + checkboxidattr + '').val('');
  }
});

【问题讨论】:

    标签: php jquery ajax api


    【解决方案1】:

    要解决此问题,您需要遵循以下脚本。

     $("input[type=checkbox]").each(function() {
          if(this).is(':checked')) {
            alert('is checked');
          } else {
            alert('is not checked');
          }
        });
    

    【讨论】:

    • 请在您的答案中添加一些解释,以便其他人可以从中学习。您的代码如何向表单添加一些验证?
    猜你喜欢
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2012-05-26
    • 2010-12-05
    • 2011-04-07
    相关资源
    最近更新 更多