简单地重用一个函数jquery

时间:2013-12-23 22:16:12

标签: jquery function selector reusability

**你好,

如何为不同的变量重用jquery函数?**

我的工作示例允许对表单进行大量提交。达到阈值时 - 该函数隐藏#form并显示#block。它还会显示剩余提交数量的计数。

我的代码只能管理一个表单 - 但我需要多种表单和阈值。

任何人都可以帮助我如何为每个表单设置单独的阈值???

每个表单(#form1,#form2,#form3)都有一个计数器(#entry_count1,#entry_count2,#entry_count3)。

我想设置:

(表格#1)

(threshold1)

(表格#2)

(threshold2)

(表格#3)

(threshold3)

...

工作代码:

<script type="text/javascript">

var threshold1 = 30; // Set this to the # of entries to trigger on
var threshold2 = 12;
var threshold3 = 24;

var form1_to_hide = ["#form1”]; // CSS selectors of forms to hide when triggered
var form2_to_hide = ["#form2”]; 
var form3_to_hide = ["#form3”]; 
var block_to_show = [“#block”]; // CSS selector of block to show when triggered


// The function
$(function(){

$('#entry_count1’).on('changed.content', function(event){
var count1 = parseInt($('.ss_entry_count_value', this).text());
 var currentCount1 =  threshold1 - count1;


if(count1 >= threshold1){


$.each(form1_to_hide, function(i)
{
$(form1_to_hide[i]).hide();
});
$.each(block_to_show, function(i){
$(block_to_show[i]).show();
});
}

  if(count1 >= threshold1)
{
$.each(form1_to_hide, function(i){
$(form1_to_hide[i]).hide();
});
$.each(block_to_show, function(i){
$(block_to_show[i]).show();
});
}

$("#result1”).text(currentCount1);

});
});

</script>

2 个答案:

答案 0 :(得分:0)

我不知道我的问题是否清楚,但这是一个镜头:

使用jquery的人做了很多事情就是使用'匿名函数'。你会看到他们是这样的:

$('someID').on('eventName', function() {
  // the anonymous function body
});

匿名函数(据我所知)是重用代码的矛盾。因为它没有名称,所以你无法真正引用它。 ......当然你总能做到

var myFunction = function() {
  // anonymous function body
};

...在'name'中捕获匿名函数。但后来我会说,只需按照“正常”的javascript方式进行操作:

function myFunction() {
  // normal function body
}

要在jquery中重用此代码,只需执行:

$('someID').on('eventName', myFunction);

它将与var中的匿名函数一起使用,并使用“普通”函数。

答案 1 :(得分:0)

最好使用数组来配置像

这样的值
<script type="text/javascript">

var threshold_config = [ 
                            {limit: 30, forms_to_hide : ["#form1"], block_to_show:["block"] },
                            {limit: 12, forms_to_hide : ["#form2"], block_to_show:["block"] },
                            {limit: 24, forms_to_hide : ["#form3"], block_to_show:["block"] }
                        ];


// The function
$(function(){

    $('#entry_count1’).on('changed.content', function(event){
    var count1 = parseInt($('.ss_entry_count_value', this).text());
    var currentCount1 =  threshold1 - count1;

    $.each(threshold_config, function(index)
    {
        var threshold1 = threshold_config[index].limit;
        var form1_to_hide = threshold_config[index].forms_to_hide;
        var block_to_show  = threshold_config[index].block_to_show;
        if(count1 >= threshold1){


            $.each(form1_to_hide, function(i)
            {
                $(form1_to_hide[i]).hide();
            });
            $.each(block_to_show, function(i){
                $(block_to_show[i]).show();
            });
        }

        if(count1 >= threshold1)
        {
            $.each(form1_to_hide, function(i){
                $(form1_to_hide[i]).hide();
            });
            $.each(block_to_show, function(i){
                $(block_to_show[i]).show();
            });
        }

        $("#result1").text(currentCount1);

    });

    });

});

</script>

这应该有效,我还没有测试过......