警报会在更新数据时逐渐弹出

时间:2017-05-09 06:14:20

标签: jquery object dynamic bootstrap-modal unbind

我有一个动态按钮来编辑对象的一些数据。现在,一旦数据成功更新,我就会向用户发出警告,说明它已成功完成。

但是,当用户第二次编辑同一个对象时,它会两次抛出相同的警报。同样,第三次更新同一个对象时需要三次,等等。

我相信,我需要取消绑定动态按钮上的click事件来修复此问题,并确保每次编辑对象时都会弹出一次警告。

此外,请注意,工具名称和工具网址默认为选项字段,但如果填写了工具名称和工具网址,则必须填写。

如何解除下面代码中'.edit-btn'按钮上的点击事件?

var myData = [{
  company: "ABC",
  url: "www.abc.com",
  type: "internal"
}, {
  company: "CDE",
  url: "www.cde.com",
  type: "internal"
}, {
  company: "DEF",
  url: "www.def.com",
  toolName: "reportTool",
  toolURL: "http://www.toolURL.com",
  type: "external"
}, {
  company: "EFG",
  url: "www.efg.com",
  type: "internal"
}, {
  company: "FGH",
  url: "www.fgh.com",
  type: "external"
}];

$('#createData').click(function() {
  createDisplay();
});

function createDisplay() {
  $('.container').empty();
  myData.forEach(function(obj) {
    $('.container').append(
      $('<div>').addClass('box').append(
        $('<label>').text('Company Website: '),
      $('<a>').addClass('compUrl').attr('href', obj.url).text(obj.company),
      obj.type == 'external' ? $('<br /><button>').addClass('edit-btn').text('Edit').attr({'data-toggle':'modal', 'data-target':'#updateData'}) : ''
      )
    )
  });
}

var objIndex;
$(document).on('click', '.edit-btn', function(){

  objIndex = $(this).parents('.box').index();

  var $toolName = $('#newToolName');
  var $toolURL = $('#newToolUrl');
  var $toolInputs = $($toolName).add($toolURL);

  $toolInputs.on('change', function(e) {

    var toolName = $toolName.val();
    var toolURL = $toolURL.val();

    $toolInputs.prop('required', toolName || toolURL);

  });


    $('#updateForm').submit(function(e){
    var toolName = $toolName.val()
    var toolURL = $toolURL.val()

    var toolFilled = !!toolName && !!toolURL;
    var toolUnfilled = !toolName && !toolURL;

    if (toolFilled || toolUnfilled) {
      updateData(objIndex);
      return false;
    }
    return false;
    });
});

function updateData(index) {
  companyName = $('#companyName').val();
  companyUrl = $('#companyUrl').val();
  toolName = $('#newToolName').val();
  toolURL = $('#newToolUrl').val();
  var upObj = {
  company: companyName,
  url: companyUrl,
  toolName: toolName,
  toolURL: toolURL,
  type: 'external'
  }
  myData.splice(index, 1, upObj);
  console.log(myData[index]);
  createDisplay();
  alert('Data updated successfully!');
}
.box {
  height: 100px;
  background-color: skyblue;
  border: 1px solid black;
  margin-top: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">

</div>

<button id="createData">Create divs</button>
<!-- Modal -->

<div class="modal fade" id="updateData" tabindex="-1" role="dialog" aria-labelledby="update-data" aria-hidden="true">
  <div class="modal-dialog">
  <div class="modal-content">
  <!-- Modal Header -->
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">
      <span aria-hidden="true">&times;</span>
      <span class="sr-only">Close</span>
      </button>
    <h4 class="modal-title" id="myModalLabel">Update Data</h4>
    </div>
    <form id="updateForm">
    <!-- Modal Body -->
      <div class="modal-body">
      <div class="form-group">
        <label class="col-sm-4 control-label" for="companyName">Company Name</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" id="companyName" placeholder="Company Name" required />
        </div>
      </div>
      <div class="form-group">
      <label class="col-sm-4 control-label" for="companyUrl">Website</label>
      <div class="col-sm-8">
        <input type="url" class="form-control" id="companyUrl" placeholder="Company URL" required />
      </div>
      </div>
        <div class="form-group">
      <label class="col-sm-4 control-label" for="newToolName">Tool Name</label>
      <div class="col-sm-8">
        <input type="text" class="form-control" id="newToolName" />
      </div>
      </div>
        <div class="form-group">
      <label class="col-sm-4 control-label" for="newToolUrl">Tool URL</label>
      <div class="col-sm-8">
        <input type="url" class="form-control" id="newToolUrl" />
      </div>
      </div>
      </div>
  <!-- Modal Footer -->
      <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      <button class="btn btn-primary">Save changes</button>
      </div>
    </form>
  </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

您可以取消绑定类似以下类的Click事件:

$(document).off('click', '.edit-btn');

unbind

附加上一个事件监听器'.edit-btn'

答案 1 :(得分:1)

删除.edit-btn点击事件,其中包含更改事件和提交事件,以便每次单击.edit-btn时绑定事件

&#13;
&#13;
var myData = [{
  company: "ABC",
  url: "www.abc.com",
  type: "internal"
}, {
  company: "CDE",
  url: "www.cde.com",
  type: "internal"
}, {
  company: "DEF",
  url: "www.def.com",
  toolName: "reportTool",
  toolURL: "http://www.toolURL.com",
  type: "external"
}, {
  company: "EFG",
  url: "www.efg.com",
  type: "internal"
}, {
  company: "FGH",
  url: "www.fgh.com",
  type: "external"
}];

$('#createData').click(function() {
  createDisplay();
});

function createDisplay() {
  $('.container').empty();
  myData.forEach(function(obj) {
    $('.container').append(
      $('<div>').addClass('box').append(
        $('<label>').text('Company Website: '),
        $('<a>').addClass('compUrl').attr('href', obj.url).text(obj.company),
        obj.type == 'external' ? $('<br /><button>').addClass('edit-btn').text('Edit').attr({
          'data-toggle': 'modal',
          'data-target': '#updateData'
        }) : ''
      )
    )
  });
}


  objIndex = $(this).parents('.box').index();

  var $toolName = $('#newToolName');
  var $toolURL = $('#newToolUrl');
  var $toolInputs = $($toolName).add($toolURL);

  $toolInputs.on('change', function(e) {

    var toolName = $toolName.val();
    var toolURL = $toolURL.val();

    $toolInputs.prop('required', toolName || toolURL);

  });
  
  $('#updateForm').submit(function(e) {
    var toolName = $toolName.val()
    var toolURL = $toolURL.val()

    var toolFilled = !!toolName && !!toolURL;
    var toolUnfilled = !toolName && !toolURL;

    if (toolFilled || toolUnfilled) {
      updateData(objIndex);
      return false;
    }
    return false;
  });
  
function updateData(index) {
  companyName = $('#companyName').val();
  companyUrl = $('#companyUrl').val();
  toolName = $('#newToolName').val();
  toolURL = $('#newToolUrl').val();
  var upObj = {
    company: companyName,
    url: companyUrl,
    toolName: toolName,
    toolURL: toolURL,
    type: 'external'
  }
  myData.splice(index, 1, upObj);
  console.log(myData[index]);
  createDisplay();
  alert('Data updated successfully!');
}
&#13;
.box {
  height: 100px;
  background-color: skyblue;
  border: 1px solid black;
  margin-top: 5px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">

</div>

<button id="createData">Create divs</button>
<!-- Modal -->

<div class="modal fade" id="updateData" tabindex="-1" role="dialog" aria-labelledby="update-data" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- Modal Header -->
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">
      <span aria-hidden="true">&times;</span>
      <span class="sr-only">Close</span>
      </button>
        <h4 class="modal-title" id="myModalLabel">Update Data</h4>
      </div>
      <form id="updateForm">
        <!-- Modal Body -->
        <div class="modal-body">
          <div class="form-group">
            <label class="col-sm-4 control-label" for="companyName">Company Name</label>
            <div class="col-sm-8">
              <input type="text" class="form-control" id="companyName" placeholder="Company Name" required />
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-4 control-label" for="companyUrl">Website</label>
            <div class="col-sm-8">
              <input type="url" class="form-control" id="companyUrl" placeholder="Company URL" required />
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-4 control-label" for="newToolName">Tool Name</label>
            <div class="col-sm-8">
              <input type="text" class="form-control" id="newToolName" />
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-4 control-label" for="newToolUrl">Tool URL</label>
            <div class="col-sm-8">
              <input type="url" class="form-control" id="newToolUrl" />
            </div>
          </div>
        </div>
        <!-- Modal Footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button class="btn btn-primary">Save changes</button>
        </div>
      </form>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;