javascript - 我如何将此 jQuery 代码转换为 jQuery 函数?

标签 javascript jquery function

我有一些 jQuery 代码,一次又一次地重复,我想通过将其转换为函数来减少我正在编写的代码。这是我正在使用的代码。

$('form#save-user button[name="save-user"]').click(function() {
    var formData = 'option=saveuser&'+$('form#save-user').serialize();
    $.ajax({
        type: 'POST',
        url:  'process.php',
        data: formData,
        success: function(msg){
            if(msg === 'empty') {
                alert('Required Values Missing');
            } else if(msg === 'duplicateEmail'){
                alert('Email already exist');
            } else {
                window.location = "index.php?users&option=edit&user_id="+msg+'&msg=success';
            }
        }
    });
});

$('form#save-user button[name="save-user-close"]').click(function() {
    var formData = 'option=saveuser&'+$('form#save-user').serialize();
    $.ajax({
        type: 'POST',
        url:  'process.php',
        data: formData,
        success: function(msg){
            if(msg === 'empty') {
                alert('Required Values Missing');
            } else if(msg === 'duplicateEmail'){
                alert('Email already exist');
            } else {
                window.location = 'index.php?users';
            }
        }
    });
});

$('form#save-user button[name="save-user-new"]').click(function() {
    var formData = 'option=saveuser&'+$('form#save-user').serialize();
    $.ajax({
        type: 'POST',
        url:  'process.php',
        data: formData,
        success: function(msg){
            if(msg === 'empty') {
                alert('Required Values Missing');
            } else if(msg === 'duplicateEmail'){
                alert('Email already exist');
            } else {
                window.location = 'index.php?users&option=create';
            }
        }
    });
});

我想知道一些事情,

a) With reference to above code, how do i convert it to function, as the code have very few changes like, selector name and url of window.location.

b) what do i call the code below, is it the function? function on the go? or dynamic function?

$('selector').event(function(){
     //jQuery Code in wake of event being triggered.
});

最佳答案

我会为它写一个小插件:

(function ($) {

jQuery.fn.myClick = function (destination) {
    this.click(function () { 
        var formData = 'option=saveuser&'+$('form#save-user').serialize();
        $.ajax({
            type: 'POST',
            url:  'process.php',
            data: formData,
            success: function(msg){
                if(msg === 'empty') {
                    alert('Required Values Missing');
                } else if(msg === 'duplicateEmail'){
                    alert('Email already exist');
                } else {
                    window.location = destination(msg);
                }
            }
        });
    });
}

}(jQuery));

然后调用它你只需要做:

$('form#save-user button[name="save-user-close"]').myClick(function() {
    return 'index.php?users&option=create';
});

$('form#save-user button[name="save-user"]').myClick(function (msg) {
    return "index.php?users&option=edit&user_id="+msg+'&msg=success';
});

你应该能够看到我们要去哪里;我们正在向我们创建的小方法添加参数,您可以在其中指定每次调用之间发生的变化。

因为在这种情况下,window.location 取决于 AJAX 响应(并且我们不知道调用该函数时的响应!),我们不能简单地提供一个字符串 (或类似的东西)作为参数。相反,我们传递一个函数,一旦收到 AJAX 响应,我们就会调用该函数,前提是 msg 作为变量;并依靠该函数提供我们设置为 window.location 的字符串。


至于你的第二个问题,你传递了一个事件处理器给jQuery事件方法;事件处理程序将是一个函数引用(通常是对匿名函数的引用)

关于javascript - 我如何将此 jQuery 代码转换为 jQuery 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6873413/

相关文章:

c - Arduino 程序资源不足

c++ - 我的功能中缺少什么吗?

javascript - 我不知道这段代码是什么

javascript - 输入范围和计算

javascript - 通过与现有行比较的表列优化 Jquery 迭代

javascript - Safari ipad 上的错误 ScrollTop

javascript - 如何使用 jQuery 在有换行符的文本周围添加跨度换行?

javascript - 如何获取数组A和B中的所有项目并将它们保存到数组C中?使用 javascript 而不使用循环

javascript - 弹出关闭不起作用有太多递归 jquery 错误

javascript - 循环遍历 jQuery 元素并如图所示对它们进行排序