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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘