duanhan3067 2014-05-04 16:08
浏览 50

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

I have a modal in an external PHP file that is included in my Ajax. The problem is that when I click to load the modal, it already submits the form, which it shouldn't (only when I press submit on the modal itself).

I've tried: e.preventDefault();, return false; $("#editForm").unbind('submit');

Also noticed a problem in the jQuery: when I click to get the id, I get the correct ID of the button (so if I edit_3, edit_5, edit_7, it will correctly alert 3, 5, 7; but the ajax form only gets the FIRST button id I clicked. So if I click edit_3, alert 3, I get 3's details; but if I click edit_5 after, I get alert 5, I get 3's details now.)

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; 
 });
});

the link that triggers:

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

And the PHP form/modal:

<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条回答 默认 最新

  • doucan8276 2014-05-04 16:21
    关注

    You can define onclick function in a tag and then call the function in that function that u made.

         <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');
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据