Why does jQuery ajax post twice here?

前端 未结 3 1195
野性不改
野性不改 2020-12-11 19:39

The following works fine the first time I select \"Add New\" and add a new option. The second time (for a different element distinguished by class) it adds the new option t

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 20:04

    Correct code :

    var Classofentry = '';
    $('#upload_form option[value="addnew"]').click(function(){
    // Show modal window
    $('#add-new').modal('show');
        Classofentry = $(this).attr("class"); // Get the 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: "main/change_options",
        data: {new_option: value, new_option_class: Classofentry},
        dataType: "html",
        error: errorHandler,
        success: success
    });
    
    function success(data){
    
      $('#'+Classofentry).append(""); 
      //alert(data);
    
      //alert('Success!');
    
    }
    
    function errorHandler(){
      alert('Error with AJAX!');
    } 
    
    $('#add-new').modal('toggle');
    
    });
    

提交回复
热议问题