Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数jquery的简单重用_Jquery_Function_Selector_Reusability - Fatal编程技术网

函数jquery的简单重用

函数jquery的简单重用,jquery,function,selector,reusability,Jquery,Function,Selector,Reusability,**你好, 如何为不同的变量重用jquery函数?*** 我的工作示例允许对一个表单进行多次提交。当达到阈值时,函数将隐藏#形式并显示一个#块。它还显示剩余提交数的计数 我的代码只能管理一个表单,但我需要多个表单和阈值 有人能帮我为每个表单单独设置阈值吗?? 每个表格(#表格1,#表格2,#表格3)都有一个计数器(#条目#计数1,#条目#计数2,#条目#计数3) 我喜欢设置: <script type="text/javascript"> var threshold1 = 30;

**你好,

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

我的工作示例允许对一个表单进行多次提交。当达到阈值时,函数将隐藏#形式并显示一个#块。它还显示剩余提交数的计数

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

有人能帮我为每个表单单独设置阈值吗??

每个表格(#表格1,#表格2,#表格3)都有一个计数器(#条目#计数1,#条目#计数2,#条目#计数3)

我喜欢设置:

<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>
(阈值1)用于(表格#1)

(阈值2)用于(表格#2)

(阈值3)用于(表格#3)

工作代码:

<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>

var threshold1=30;//将此设置为要触发的条目的#
var阈值2=12;
var阈值3=24;
var form1_to_hide=[“#form1”];//触发时要隐藏的表单的CSS选择器
var form2_to_hide=[“#form2”];
var form3_to_hide=[“#form3”];
var block_to_show=[“#block”];//触发时要显示的块的CSS选择器
//功能
$(函数(){
$('#entry_count1')。on('changed.content',function(event){
var count1=parseInt($('.ss_entry_count_value',this).text());
var currentCount1=阈值1-计数1;
如果(计数1>=阈值1){
$.each(格式1到隐藏,函数(i)
{
$(form1_to_hide[i]).hide();
});
$。每个(块到显示,功能(i){
$(block_to_show[i]).show();
});
}
如果(计数1>=阈值1)
{
$.each(格式1到隐藏,函数(i){
$(form1_to_hide[i]).hide();
});
$。每个(块到显示,功能(i){
$(block_to_show[i]).show();
});
}
$(“#结果1”).text(currentCount1);
});
});

我不知道我是否明白了你的问题,但这里有一个镜头:

使用jquery的人经常使用的是“匿名函数”。您将看到它们是这样的:

$('someID').on('eventName', function() {
  // the anonymous function body
});
据我所知,匿名函数是重复使用代码的矛盾。因为它没有名字,所以你不能真正提到它。。。当然,你总能做到

var myFunction = function() {
  // anonymous function body
};
。。。在“名称”中捕获匿名函数。但我会说,只需使用“正常”的javascript方式即可:

function myFunction() {
  // normal function body
}
要在jquery中重用此代码,只需执行以下操作:

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

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

最好使用数组来配置如下值:

<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>

var阈值_配置=[
{limit:30,forms_to_hide:[“#form1”],block_to_show:[“block”]},
{limit:12,forms#u to_hide:[“#form2”],block#u to_show:[“block”]},
{limit:24,forms#u to_hide:[“#form3”],block#u to_show:[“block”]
];
//功能
$(函数(){
$('#entry_count1')。on('changed.content',function(event){
var count1=parseInt($('.ss_entry_count_value',this).text());
var currentCount1=阈值1-计数1;
$.each(阈值配置、函数(索引)
{
var threshold1=阈值配置[索引]。限制;
var form1\u to\u hide=阈值配置[索引]。forms\u to\u hide;
var block_to_show=阈值配置[索引]。block_to_show;
如果(计数1>=阈值1){
$.each(格式1到隐藏,函数(i)
{
$(form1_to_hide[i]).hide();
});
$。每个(块到显示,功能(i){
$(block_to_show[i]).show();
});
}
如果(计数1>=阈值1)
{
$.each(格式1到隐藏,函数(i){
$(form1_to_hide[i]).hide();
});
$。每个(块到显示,功能(i){
$(block_to_show[i]).show();
});
}
$(“#结果1”).text(currentCount1);
});
});
});
这应该行得通,我还没有测试过