如何合并两个相同的jQuery脚本?

时间:2011-11-14 23:46:29

标签: javascript jquery

下面是我用来在DB中添加一些条目的jQuery。它工作正常。我在同一个文件中使用相同的一个来删除一些数据。 remove-from-db 的格式为RemoveMe,并发布到$.post("add-to-db.php?id=<?php echo (int)$_GET['id'] ?>"

您可以在下面看到格式为AddMe add-to-db 脚本。

我的问题是,是否有办法将这两者结合在一起。谢谢。

$(function(){
    $("#AddMe").submit(function(e){
       e.preventDefault();   
        $.post("add-to-db.php?id=<?php echo (int)$_GET['id'] ?>", $("#AddMe").serialize(), function (data){  
            $("#submit").attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif');
            $("#submit").attr('disabled',true);
            $("#submit").unbind('click');
            $("#message_post").html("Thank you");
setTimeout(function () { $("#message_post").hide(); window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; }, 2000);

        });
    });
});

<form id="AddMe">
<input type="image" name="submit" id="submit" src="http://blog.mirthlab.com/wp-content/uploads/2008/04/rollover-up.png">
</form>
<div id="message_post"></div>

$(function(){
    $("#RemoveMe").submit(function(e){
       e.preventDefault();   
        $.post("remove-from-db.php?id=<?php echo (int)$_GET['id'] ?>", $("#RemoveMe").serialize(), function (data){          
            $("#submit").attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif');
            $("#submit").attr('disabled',true);
            $("#submit").unbind('click');
            $("#message_post").html("Thank you");
setTimeout(function () { $("#message_post").hide(); window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; }, 2000);
        });
    });
});

<form id="RemoveMe">
<input type="image" name="submit" id="submit" src="http://blog.mirthlab.com/wp-content/uploads/2008/04/rollover-up.png">
</form>
<div id="message_post"></div>

3 个答案:

答案 0 :(得分:3)

您可以使用对象文字数组来存储id和url对

$(function () {   
    var buttons = [
      { id : '#AddMe', url : 'add-to-db.php' }, 
      { id : '#RemoveMe', url : 'remove-from-db.php' }
    ];

    $.each(buttons, function(i,v) {
        $(v.id).submit(function(e){
           e.preventDefault();   
            $.post(v.url + "?id=<?php echo (int)$_GET['id'] ?>", $(v.id).serialize(), function (data) {  
                $("#submit")
                    .attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif')
                    .attr('disabled',true)
                    .unbind('click');

                $("#message_post").html("Thank you");

                setTimeout(function () { 
                    $("#message_post").hide(); 
                    window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; 
                }, 2000);

            });
        });
    });
});

答案 1 :(得分:0)

如上所述使用它并删除它:

});



$(function(){

答案 2 :(得分:0)

是的,你可以改变这个:

$( function () {
    // code block A
});

$( function () {
    // code block B
});

进入这个:

$( function () {
    // code block A
    // code block B
});
相关问题