为什么jQuery ajax在这里发布两次?

Mys*_*ist 5 ajax jquery

第一次选择"添加新"并添加新选项时,以下工作正常.第二次(对于按类别区分的不同元素),它将新选项添加​​到所选元素和第一个元素.这两个元素必然会重新出现.

  <script type="text/javascript">

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

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

          // Get the class

          var Classofentry = $(this).attr("class");           

          $('#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').modal('toggle');

           });
      }); 

  </script>
Run Code Online (Sandbox Code Playgroud)

奇怪的是,它似乎两次在ajax上传了一个帖子.我想它找到了所有"addnew"值(到目前为止有2个,会有更多).如何使用指定的类处理元素?希望这是有道理的.

Mys*_*ist 4

感谢您的回复!我找到了一种方法,通过保留点击嵌套但解除第二个点击的绑定来使其工作。我无法让建议的解决方案(取消所有功能的嵌套)工作。当它们没有嵌套时,似乎没有办法让第二次单击起作用。我不知道为什么。调用 ajax 的函数内还需要有 success 和 errorHandler 函数。这是代码(与上面的问题相同,但在第二次嵌套单击中使用取消绑定语句):

  <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 = $(this).attr("class");
          console.log(Classofentry);Thanks            

        $('#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
              });

          $('#add-new-submit').unbind('click') // <-------------- The answer!!!!!
          $('#add-new').modal('toggle');


          function success(data)
          {

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

              //alert('Success!');

          }

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

  </script>
Run Code Online (Sandbox Code Playgroud)