重写这个函数的聪明方法

发布于 2024-10-31 10:18:25 字数 539 浏览 0 评论 0原文

我有这个,如果用户单击一个按钮,我将显示一个 div,如果用户单击其他按钮,则不会显示它。它有效,但重复这样做是愚蠢的

$j(document).ready(function() {
    $j('#Button1').click( function () { 
        var data = $j("form").serialize();
        $j.post('file.php', data, function(response){       
            $j("#Response").show();
        });
    });
    $j('#Button21').click( function () { 
        var data = $j("form").serialize();
        $j.post('file.php', data, function(response){       
            //do something else          

        });
    });
}); 

I have this, and I am showing a div if user clicked one button and not showing it if the user clicked other. Its working but its dumb to do this way with repeatition

$j(document).ready(function() {
    $j('#Button1').click( function () { 
        var data = $j("form").serialize();
        $j.post('file.php', data, function(response){       
            $j("#Response").show();
        });
    });
    $j('#Button21').click( function () { 
        var data = $j("form").serialize();
        $j.post('file.php', data, function(response){       
            //do something else          

        });
    });
}); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泪是无色的血 2024-11-07 10:18:25

我会通过向选定的按钮添加一个类,然后从 click 函数中提取 event.target id 来完成此操作:

    $j('.buttons').click(function(e) {
        var buttonId = e.target.id,
            data = $j("form").serialize();

        $j.post('file.php', data, function(response) {
            switch (buttonId) {
                case "Button1":
                    $j("#Response").show();
                    break;
                case "Button21":
                    //do something else
                    break;
            }
        });
    });

I'd do it by adding a class to the selected buttons and then pull the event.target id from the click function:

    $j('.buttons').click(function(e) {
        var buttonId = e.target.id,
            data = $j("form").serialize();

        $j.post('file.php', data, function(response) {
            switch (buttonId) {
                case "Button1":
                    $j("#Response").show();
                    break;
                case "Button21":
                    //do something else
                    break;
            }
        });
    });
始终不够爱げ你 2024-11-07 10:18:25

您需要从功能中抽象数据。

sendClick('#Button1', function() {
  $j('#Response').show();
});

sendClick('#Button21', function() {
  // do something
});

sendClick 函数

function sendClick(selector, callback)
{
  $j(selector).click( function () { 
    var data = $j("form").serialize();
    $j.post('file.php', data, callback);
  });
}

通过这种方式,您可以通过更改选择器和回调来一遍又一遍地重复相同的功能。您可以通过以下方式进一步自定义:

function sendClick(selector, options, callback)
{
  // handle arguments
  if(typeof options == 'function') { 
    callback = options;
    options = {};
  } else {
    options = options || {};
  }

  $j.extend({
    form: 'form',
    file: 'file.php'
  }, options);

  // abstracted logic
  $j(selector).click(function() { 
    var data = $j(options.form).serialize();
    $j.post(options.file, data, callback);
  });
}

然后使用 like

sendClick('#select', {form: '#anotherForm'}, function() {
  // do something
});

sendClick('#another', function(response) {
  // something else
});

You need to abstract the data from the functionality.

sendClick('#Button1', function() {
  $j('#Response').show();
});

sendClick('#Button21', function() {
  // do something
});

sendClick function

function sendClick(selector, callback)
{
  $j(selector).click( function () { 
    var data = $j("form").serialize();
    $j.post('file.php', data, callback);
  });
}

This way you can repeat the same functionality over and over by changing the selector and the callback. You could customise this even further by:

function sendClick(selector, options, callback)
{
  // handle arguments
  if(typeof options == 'function') { 
    callback = options;
    options = {};
  } else {
    options = options || {};
  }

  $j.extend({
    form: 'form',
    file: 'file.php'
  }, options);

  // abstracted logic
  $j(selector).click(function() { 
    var data = $j(options.form).serialize();
    $j.post(options.file, data, callback);
  });
}

then use like

sendClick('#select', {form: '#anotherForm'}, function() {
  // do something
});

or

sendClick('#another', function(response) {
  // something else
});
网白 2024-11-07 10:18:25

您可以将事件附加到两者,然后,当您需要检查哪个元素触发了事件时,请使用 <代码>事件.目标

$j(function() {
    $j('#Button1, #Button2').click( function (event) { 
        var data = $j("form").serialize();

         $j.post('file.php', data, function(response){       

             if ($(event.target).is('#Button1')) {
                 $j("#Response").show();
             } else {
                 // Do something else          
             }

         });
    });
});

You can attach the event to both, and then, when you need to check which element triggered the event, use event.target.

$j(function() {
    $j('#Button1, #Button2').click( function (event) { 
        var data = $j("form").serialize();

         $j.post('file.php', data, function(response){       

             if ($(event.target).is('#Button1')) {
                 $j("#Response").show();
             } else {
                 // Do something else          
             }

         });
    });
});
ㄖ落Θ余辉 2024-11-07 10:18:25

这里有两种不同的方法:


您可以将两个处理程序合并为一个处理程序:

$j(document).ready(function () {
    $j('#Button1, #Button21').click(function() {
        var id = this.id;
        var data = $j("form").serialize();
        $j.post('file.php', data, function(response) {
            if (id == 'Button1') {
                // Show
            } else {
                // Do something else
            }
        });
    });
});

或者编写一种特殊类型的处理程序:

$j.fn.clickAndPost = function (handler) {
    this.click(function () {
        var me = this;
        var data = $j("form").serialize();
        $j.post('file.php', data, function(response) {
            handler.call(me);
        });
    });
});

...并附加其中两个:

$j(document).ready(function () {
    $j('#Button1').clickAndPost(function () {
        // Show
    });

    $j('#Button21').clickAndPost(function () {
        // Do something else
    });
});

Here are two different ways:


You can combine the two handlers into one handler:

$j(document).ready(function () {
    $j('#Button1, #Button21').click(function() {
        var id = this.id;
        var data = $j("form").serialize();
        $j.post('file.php', data, function(response) {
            if (id == 'Button1') {
                // Show
            } else {
                // Do something else
            }
        });
    });
});

Or write a special kind of handler:

$j.fn.clickAndPost = function (handler) {
    this.click(function () {
        var me = this;
        var data = $j("form").serialize();
        $j.post('file.php', data, function(response) {
            handler.call(me);
        });
    });
});

...and attach two of them:

$j(document).ready(function () {
    $j('#Button1').clickAndPost(function () {
        // Show
    });

    $j('#Button21').clickAndPost(function () {
        // Do something else
    });
});
未央 2024-11-07 10:18:25
$j(function($) {

    $('#Button1', '#Button21').click(function() {
        var that = this,
            data = $('form').serialize();

        $.post('file.php', data, function(response) {

            if ( that.id === 'Button1' ) {
                $('#Response').show();
            } else {
                //do something else   
            }

        });
    });

});
$j(function($) {

    $('#Button1', '#Button21').click(function() {
        var that = this,
            data = $('form').serialize();

        $.post('file.php', data, function(response) {

            if ( that.id === 'Button1' ) {
                $('#Response').show();
            } else {
                //do something else   
            }

        });
    });

});
清引 2024-11-07 10:18:25
$(document).ready(function() {

    $('#Button1 #Button21').click(function() {
        var that = this.attr("id");
            data = $('form').serialize();

        $.post('file.php', data, function(response) {

            if ( that === 'Button1' ) {
                $('#Response').show();
            } else {
                //do something else   
            }

        });
    });

});

如果不起作用请告诉我。

$(document).ready(function() {

    $('#Button1 #Button21').click(function() {
        var that = this.attr("id");
            data = $('form').serialize();

        $.post('file.php', data, function(response) {

            if ( that === 'Button1' ) {
                $('#Response').show();
            } else {
                //do something else   
            }

        });
    });

});

Let me know if it's not working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文