0

ドロップダウンから「新規追加」を選択するとポップアップするモーダルウィンドウがあります。ユーザーは、ドロップダウンに追加する新しいオプションをテキスト フィールドに入力できます。テキストが実際に新しいフィールドのそれぞれに毎回入力されている限り、問題なく動作します (この素晴らしいコミュニティの助けに感謝しますなぜ jQuery ajax がここに 2 回投稿するのですか? :-) 今、私の問題は、ユーザーがクリックしてモーダルを閉じた場合です。その外側で別の「新規追加」を選択してテキストを入力しようとすると、追加ボタンをクリックすると、上記の以前にリンクされた質問のように、無関係なアクションがたくさんあります。明らかに、バインドを解除する必要がありますが、私にはわかりません。理想的には、ユーザーが任意の数のモーダル ウィンドウを開いたり閉じたりでき、データを入力できる必要があります。何か案は?

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 -->
4

1 に答える 1