jQuery ajax模式在加载时提交表单

时间:2014-05-04 16:08:37

标签: javascript php jquery ajax

我在Ajax中包含的外部PHP文件中有一个模态。问题是,当我点击加载模态时,它已经提交了表单,它不应该提交(只有当我按下模态本身的提交时)。

我试过了:e.preventDefault();,返回false; $( “#editForm”)解除绑定( '提交');

还注意到jQuery中的一个问题:当我点击获取id时,我得到了正确的按钮ID(所以如果我edit_3,edit_5,edit_7,它将正确警告3,5,7;但是ajax表单只获取我点击的第一个按钮ID。所以,如果我点击edit_3,警告3,我得到3的详细信息;但如果我点击edit_5之后,我得到警报5,我现在得到3的详细信息。)

的jQuery / AJAX:

$(function() {
 $( ".edit" ).click(function(e){
    var type_id = $(this).attr( "name" );
    type_id = type_id.replace('edit_', '');     

    //e.preventDefault(); ajax also only sends ID of first edit I click, 
      // not the current one.
    $.ajax({
        type: "POST",
        url: "/type/edit.php",
        cache: false,
        data: {'type_id': type_id}
    }).done(function(html) {
        $('body').append(html);
        $('#modalEditType').modal('show');
    });

    //return false; 
 });
});

触发的链接:

<a href="#modalEditType" class="edit" name="edit_<?php echo $row['id'];?>"><button class="btn btn-default" type="button"><i class="fa fa-edit"></i></button></a>

PHP表单/模式:

<form class="international" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="form-join">
   <div class="row col-lg-12">
    <div class="form-group">
    <label class="control-label col-lg-3">Type Name</label>
    <input type="text" id="title" name="title" placeholder="Type Name" class="form-control" value="<?php echo $type['title']; ?>" required />
 </div>
 <div class="col-sm-12">
   <button class="btn btn-danger" type="submit" name="submitted" value="" >Edit Type</button>
   <button class="btn btn-info" type="submit" name="cancel" value="Cancel Update" onclick="$('#modalEditType').modal('hide');">Cancel</button>
  </div>
    <input type="hidden" value="0" name="submitme"/>
   </form>

3 个答案:

答案 0 :(得分:1)

嗯,您可以原生地阻止表单提交,就像这样,

$("form.international").submit(function(event){
    event.preventDefault();
});

这会阻止触发提交表单的任何内容

答案 1 :(得分:0)

您可以在标签中定义onclick函数,然后在您创建的函数中调用该函数。

     <a href="#modalEditType" class="edit" onclick="sub_form()" name="edit_<?php echo $row['id'];?>"><button class="btn btn-default" type="button"><i class="fa fa-edit"></i></button></a>

function sub_form()
{
 var type_id = $(this).attr( "name" );
    type_id = type_id.replace('edit_', '');     

    //e.preventDefault();
    $.ajax({
        type: "POST",
        url: "/type/edit.php",
        cache: false,
        data: {'type_id': type_id}
    }).done(function(html) {
        $('body').append(html);
        $('#modalEditType').modal('show');
    });
}

答案 2 :(得分:0)

我决定重写代码(PHP)方面。除了现在没有正确发送id(另一个问题的另一个问题),代码在加载时不再提交。重写后我最好的猜测是两个按钮:

  <button class="btn btn-danger" type="submit" name="submitted" value="" >Edit Type</button>
   <button class="btn btn-info" type="submit" name="cancel" value="Cancel Update" onclick="$('#modalEditType').modal('hide');">Cancel</button>

无论出于何种原因(如果我发现,我将重新编辑),即使取消也没有隐藏模态,而是提交表单。我将其转换为链接:

   <a href="" class="btn btn-info" onclick="$('#modalEditType').modal('hide');">Cancel</a>

它似乎有所帮助。接受这个答案和djay's都是正确的。

相关问题