我的下拉下拉更改事件在我选择select时第一次发生两次,而在提交时第二次发生。所以我想停止第二次调用change事件

时间:2019-01-18 07:04:57

标签: javascript jquery

我的下拉列表更改事件在我选择select时第二次发生,在提交时第二次发生。所以我想停止第二次更改事件的调用,当我单击Submit时。第一次提交时已经发生更改 然后只需重新输入所选数据,但将所选复选框设置为空即可。

$(document).ready(function() {
  $('input[name=DepartmentId]').change(function() {
    var departmentId = $('input[name=DepartmentId]:checked').val();
    $.ajax({
      url: urlGetDepartmentTechnology,
      data: {
        DepartmentId: departmentId
      },
      datatype: "json",
      type: "GET",
      contentType: "application/json",
      success: function(data) {

        if (data != null && data != "") {

          $("#technologyID").html("");
          $.each(data, function(i, record) {
            $("#technologyID").append(`<input type='checkbox' id='${this.TechnologyId}' name='${this.TechnologyName}'/>${this.TechnologyName}`);
          });
        } else {
          $("#technologyID").html("");
          $('#technologyID').html("<b>No Technology exists with Department < /b>");
        }
      },
      error: function() {
        $("#TechnologyId").append('<option value="">No Data Found</option>');
      }
    });
  });

  $("#btn").click(function() {

    var list = [];
    $('#technologyID input:checked').each(function() {
      var id = $(this).val();
      list.push(this.id);
    });
    var list1 = list;
    alert(list)
    var values = new Object();
    values.TechnologyIdList = list1;
    values.DepartmentId = DepartmentId;
    values.SkillName = SkillName;
    values.Description = Description;
    values.ReferenceURL = ReferenceURL;
    values.IsActive = IsActive;
    values.IsRequiredInMentorship = IsRequiredInMentorship;

    $.ajax({
      type: 'POST',
      url: urlGetTechnology,
      data: {
        skillMaster: values
      },
      success: function(result) {
        alert("success");
        return false;
      },
      error: function(result) {
        alert("error!");
      }
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

0 个答案:

没有答案