模态窗口和绑定/解除绑定

时间:2013-07-17 18:32:37

标签: jquery html

当从下拉列表中选择“添加新内容”时,我会弹出一个模态窗口。用户可以在文本字段中键入要添加到下拉列表的新选项。只要文本实际上每次输入到每个新字段中都可以正常工作(感谢来自这个令人敬畏的社区的帮助Why does jQuery ajax post twice here? :-)现在我的问题是如果用户通过点击外部来解除模态然后选择另一个“添加新”并尝试输入文本,当单击添加按钮时,会有一堆无关的操作,就像上面之前链接的问题一样。显然需要做一些解除束缚,但我无法弄明白。理想情况下,用户应该能够打开和关闭任意数量的模态窗口,并且仍然能够输入数据。有什么想法吗?

这是jQuery:

  <script type="text/javascript">

        var Classofentry = '';

        $('#upload_form option[value="addnew"]').click(function(){

              // Show modal window
              $('#add-new').modal('show');

              // Get the class
              //var Classofentry = $('#upload_form option[class]').attr("class");
              var Classofentry = $(this).attr("class");
              //console.log(Classofentry);


              $('#add-new-submit').on('click', function(){                

                  // Get new option from text field
                  var value = $('#add-new-text').val();
                  //console.log(value);

                  $.ajax({
                        type: "POST",
                        url: "<?php echo site_url(); ?>main/change_options",
                        data: {new_option: value, new_option_class: Classofentry},
                        dataType: "html",
                        error: errorHandler,
                        success: success
                      });

                  function success(data)
                  {

                      $('#'+Classofentry).append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); 
                      //alert(data);

                      //alert('Success!');

                  }

                  function errorHandler()
                  {
                      alert('Error with AJAX!');
                  } 

                  $('#add-new-submit').unbind('click'); // This fixes the problem for multiple entries
                  //$('#add-new-text').unbind('click');
                  //$('#upload_form option[value="addnew"]').unbind('click')
                  $('#add-new').modal('toggle');                      

              });

              //$('#add-new-submit').unbind('click');

              //$('#upload_form option[value="addnew"]').unbind('click');
        });


  </script>

这是模式:

  <!-- add-new field -->
  <div class="modal small hide fade" id="add-new" tabindex="-1" role="dialog" aria-labelledby="add-new-fieldLabel" aria-hidden="true">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="add-new-fieldLabel">Add New Field</h3>
      </div>
      <div class="modal-body">

          <p>Would you like to add a new item?</p>
          <input type="text" id="add-new-text" name="add-new-text" placeholder="Type the new option">

      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-success" id="add-new-submit" name="add-new-submit"/>Add</button>
      </div>
 </div><!-- /add-new field -->

1 个答案:

答案 0 :(得分:0)

您需要将事件处理程序绑定到DOM中未被替换的内容,即<body>

$(document.body).on('click', '#someElement', function() {
    // do something
});

您还可以绑定到另一个您知道不会被替换的父标记。

http://api.jquery.com/on/