2012-10-31 92 views
0

我有这个jquery ajax表单提交脚本,我写在DB中创建和编辑项目。jquery和ajax表单提交到MySQL数据库中的双重条目

问题是,每当我创建一个项目2项目创建。

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#new_item').on('hidden', function() { 
      $(".alert").hide(); // remove errors…when the modal form closes 
     }); 
     $(".showedit").click(function(){ 
      var edit = $(this).attr('id').split("_");// get the edit id 
      var id = edit[1]; 

      var values = $('#data_'+id+' td').map(function(_, td) { 
       return $(td).text(); 
      }).get(); 
      // populate the form with the database field 
      var name = $("input[name=name]").val(values[1]); 
      var model = $("select[name=model]").val(values[2]); 
      var brand = $("select[name=brand]").val(values[3]); 
      $('#new_item').modal(); 
     }); 

     $("#submit_button").click(function(e) { 
      e.preventDefault(); 

      var name = $("input[name=name]").val(); 
      var amount = $("input[name=amount]").val(); 
      var model = $("select[name=model]").val(); 
      var brand = $("select[name=brand]").val(); 

      $.ajax({    
        type: "POST",     
        url: "<?=base_url()?>items/create",     
        cache: false, 
        dataType: "json", 
        data: 'name='+name+'&amount='+amount+'&model='+model+'&brand='+brand, 
        success: function(result){ 
         if(result.error) { 
          $(".alert").addClass('alert-error'); 
          $(".alert").fadeIn('slow').html(result.message); 
          } 
          else 
          {     
          $(".alert").addClass('alert-success'); 
          $(".alert").fadeIn('slow').html(result.message); 
          } 

        } 
      }); 

     }); 
    }); 
</script> 

请帮我解决这个问题。我会很棒!

+0

你检查你的控制台形式...? – chhameed

+0

你也可以尝试改变'$(“#submit_button”)。submit(function(e){' – chhameed

+1

你能添加你的html代码吗? – hendrathings

回答

0
$("#submit_button").click(function(e) { 
      e.preventDefault(); 

它意味着它会阻止按钮默认动作

你必须改变这个作为

$("form").submit(function(e) { 
      e.preventDefault(); 

,或者你可以将ID添加到