简体   繁体   English

我的第一个jQuery插件,似乎只绑定到页面上八种形式的第一种形式

[英]my first jQuery plugin, only seems to bind to the first form of eight forms on the page

I'm writing a small jQuery plugin that binds to 8 divs on a page, each of which contains a form. 我正在写一个小的jQuery插件,该插件绑定到页面上的8个div,每个div都包含一个表单。 There are two links for each widget that increment or decrement a field within the form and then POSTs the form. 每个窗口小部件都有两个链接,这些链接会递增或递减表单中的字段,然后发布表单。 The problem is that all 16 links on the page all submit the first form on the page. 问题在于页面上的所有16个链接都提交了页面上的第一个表单。

The source code is here: http://pastie.org/657045 源代码在这里: http : //pastie.org/657045

I'm a jQuery/JS newb and figure this is probably a variable scope issue, but I've tried everything and can't get the elements to operate independently. 我是jQuery / JS newb,认为这可能是一个范围可变的问题,但是我尝试了所有操作,但无法使这些元素独立运行。

At the very beginning, you need to iterate through the results of the $(containerobj) and run the rest of the code through that, replacing each reference, like this: 从一开始,您就需要遍历$(containerobj)的结果,并通过它运行其余代码,替换每个引用,如下所示:

jQuery.RankWidget = function(containerobj, options) {
   return $(containerobj).each(function() {
     var $container = $(this);
     var $form          = $container.find('.rank_buttons form');
     var $rank_up       = $container.find('.rank.up');
     var $rank_down     = $container.find('.rank.down');
     var $value_field   = $container.find('input.rank_value');
     var $comment_score = $container.find('.comment_score');
     var $comment_rank  = $container.find('.comment_rank');
     ...
   };
};

At the moment, you're applying all the events to all of the links, rather than scoping it to each container object in turn. 目前,您正在将所有事件应用于所有链接,而不是依次将其限定在每个容器对象上。

I solved my problem by using .click() instead of .live() 我通过使用.click()而不是.live()解决了我的问题

Anyone know if there is away to use .live() and get the scope for each widget correct? 有人知道是否可以使用.live()并正确获取每个小部件的作用域? I even tried passing in the variable as an argument to the clickRank() function, but it was still referencing the wrong form. 我什至尝试将变量作为参数传递给clickRank()函数,但它仍引用错误的形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM