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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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 - Fatal编程技术网

将Jquery代码重构为函数以便重用

将Jquery代码重构为函数以便重用,jquery,Jquery,如果可能的话,寻求一些帮助 我已经创建了一些jquery代码来完成这项工作,但是我觉得有点笨拙,我想正确地重构它 当代码响应用户的点击时,它会在服务器上检查是否允许用户投票,如果允许,它会通过ajax处理投票,并相应地移动投票 但是,它当前设置为仅在单击“向上投票”链接时运行,在该链接中,它会向投票计数中添加一个。我还想做的是,当用户单击class=“vote down”链接时,从选票计数中减损1,但我不想再次重复相同的代码 想知道是否有必要将所有代码打包到一个函数中,并说“如果点击了投票,则添

如果可能的话,寻求一些帮助

我已经创建了一些jquery代码来完成这项工作,但是我觉得有点笨拙,我想正确地重构它

当代码响应用户的点击时,它会在服务器上检查是否允许用户投票,如果允许,它会通过ajax处理投票,并相应地移动投票

但是,它当前设置为仅在单击“向上投票”链接时运行,在该链接中,它会向投票计数中添加一个。我还想做的是,当用户单击class=“vote down”链接时,从选票计数中减损1,但我不想再次重复相同的代码

想知道是否有必要将所有代码打包到一个函数中,并说“如果点击了投票,则添加一个,如果点击了投票,则删除一个”

非常感谢

<a href="link class="vote up"> Vote Up </a>
<a href="link class="vote down"> Vote Down</a>




        $('body#true .voteUp').click(function(){

        // Get the song meaning
        $thisLink = $(this);
        var idSm = $(this).parents("div:eq(1)").attr("id");
            //Validate that user isnt rating their own song meaning
            $.getJSON('http://localhost:8500/mxRestore/model/mdl_songs.cfc?method=getRateSm&returnFormat=json&queryformat=column', 
            {idSm: idSm}, 
            function(data){
                var bVoteAllowed = data.ROWCOUNT < 1;
                if(bVoteAllowed){
                // User can vote
                    $.getJSON('http://localhost:8500/mxRestore/model/mdl_songService.cfc?method=rateSm&returnFormat=json&queryformat=column', 
                    {idSm:idSm,action:true})
                                   // Change vote accordingly
                    var totalQuantity = 0;
                    var quantity = $thisLink.parent().parent().children('.rateValue').text();
                    quantity = parseInt(quantity);
                    totalQuantity = quantity + 1; 
                    $thisLink.parent().parent().children('.rateValue').text(String(totalQuantity)).effect("highlight", {}, 3000);
                }else {

                    $thisLink.text("you are not allowed to vote")   
                }

            })
            return false

    })

$('body#true.voteUp')。单击(函数(){
//明白这首歌的意思吗
$thisLink=$(此);
var idSm=$(this).parents(“div:eq(1)”).attr(“id”);
//验证用户是否未对自己的歌曲含义进行评分
$.getJSON('http://localhost:8500/mxRestore/model/mdl_songs.cfc?method=getRateSm&returnFormat=json&queryformat=column', 
{idSm:idSm},
功能(数据){
var bVoteAllowed=data.ROWCOUNT<1;
如果(允许){
//用户可以投票
$.getJSON('http://localhost:8500/mxRestore/model/mdl_songService.cfc?method=rateSm&returnFormat=json&queryformat=column', 
{idSm:idSm,action:true})
//相应地改变投票
var totalQuantity=0;
变量数量=$thisLink.parent().parent().children('.rateValue').text();
数量=parseInt(数量);
总数量=数量+1;
$thisLink.parent().parent().children('.rateValue').text(String(totalQuantity)).effect(“highlight”,{},3000);
}否则{
$thisLink.text(“不允许您投票”)
}
})
返回错误
})

用已定义的函数体替换
。您可以使用传递给
vote()的值
来决定它应该被投赞成票还是反对票。

body#true
是不必要的,而且会减慢速度。只需使用
#true
。更好的是,我会这样写
#true a.voteUp
谢谢你的建议…为什么这是一个改进的方法?谢谢你,谢谢你的帮助。似乎有点小问题但是,对于代码。每当我调用它时,它似乎都不会识别投票函数中的“idSm”变量。它说该变量未定义……你知道为什么吗?谢谢,你能更明确地说明这段代码吗?因为我似乎仍然会在if($(this).hasClass('voteUp')中重复大量代码{//perform vote up action}否则{//perform vote down action}返回false;部分。非常感谢
$('.voteUp, .voteDown').click(function(){
   //do your processing, vote checking etc


    if ($(this).hasClass('voteUp')){
       //perform vote up action
    }
    else{
        //perform vote down action
    }
   return false;
});
$('.voteUp, .voteDown').click(function(){
   //do your processing, vote checking etc


    if ($(this).hasClass('voteUp')){
       //perform vote up action
    }
    else{
        //perform vote down action
    }
   return false;
});