使用jQuery ajax表单提交

时间:2013-05-29 08:00:26

标签: javascript ajax jquery

单击li时,不使用ajax.form提交表单。给我一些解决方案

我的js代码在这里

$(document).ready(function(){

$('#sortable li').click(function() {

$("#frmgallery").submit(function(event) {
    event.preventDefault();
    var formdata = $(this).serialize();
    alert(formdata);
    $.ajax({
        type: "POST",
        url: "gallery.php",
        data: formdata,
        success: function(){alert('success');}
    });
});

});

HTML就在这里

 <form method="post" enctype="multipart/form-data" id="frmgallery" name="gallery" action="<?php get_template_directory();?>admin.php?page=gallery/gallery.php">

<ul id="sortable">

Query

<li  class="ui-state-default" name='liindex' id="<?php echo $row['glryRecordId'];?>" >
<span style="display:none"><?php echo $row['glryRecordId'];?></span>
<img class='thumbnail' alt=''  src='<?php echo get_site_url();?>/wp-content/themes/townsley/upload/<?php echo $row['glryName']; ?>' width='80' height='60' 
 style="border: 1px solid #D3D3D3;padding:2px;"/><input type="hidden" value="<?php echo $row['glryRecordId'];?>" name="recordId[]" />
    <a href="<?php get_template_directory();?>admin.php?page=gallery/gallery.php&delid=<?php echo $row['glryRecordId'];?>" style="display:block;text-align:center"  title="DELETE this image from the record" class="arial-red">Remove</a>
</li>


</ul>
</form> 

请帮帮我 感谢

ajax jquery javascript

4 个答案:

答案 0 :(得分:2)

您也应该在问题中提供HTML,但据我所知,您在event回调中event实际上没有任何内容可以发起submit事件。所以基本上你应该考虑这样的事情:

$(document).ready(function(){

    $('#sortable li').click(function() {
        event.preventDefault();
        var formdata = $("#frmgallery").serialize();
        alert(formdata);
        $.ajax({
            type: "POST",
            url: "gallery.php",
            data: formdata,
            success: function(){alert('success');}
        });
    });

});

答案 1 :(得分:0)

您可以使用Ajax Form Plugin中的ajaxForm / ajaxSubmit函数或jQuery序列化函数。

示例:

$("#frmgallery").ajaxForm({url: 'gallery.php', type: 'post'})

$("#frmgallery").ajaxSubmit({url: 'gallery.php', type: 'post'})
按下提交按钮时,

ajaxForm将发送。 ajaxSubmit立即发送。

答案 2 :(得分:0)

您是否尝试在提交功能结束时返回false?

    $("#frmgallery").submit(function(e) {
        e.preventDefault();
        var formdata = $(this).serialize();
        alert(formdata);
        $.ajax({
            type: "POST",
            url: "gallery.php",
            data: formdata,
            success: function(){alert('success');}
            error: function(){alert('error');}
        });
        return false;
    });
$('#sortable li').click(function() {

     $("#frmgallery").submit();
});

同时发布您从$.ajax电话

获得的内容

答案 3 :(得分:0)

$("#frmgallery").ajaxForm({url: 'gallery.php', type: 'post'})

您是否尝试在提交功能结束时返回false?

    $("#frmgallery").submit(function(e) {
        e.preventDefault();
        var formdata = $(this).serialize();
        alert(formdata);
        $.ajax({
            type: "POST",
            url: "gallery.php",
            data: formdata,
            success: function(){alert('success');}
            error: function(){alert('error');}
        });
        return false;
    });
$('#sortable li').click(function() {

     $("#frmgallery").submit();
});