首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用插件创建和扩展 jQuery 函数-5

使用插件创建和扩展 jQuery 函数-5

计算我发现的另外一个在某些情况下很有用的插件是 Calculation 插件。与大多数优秀的插件一样,它也只是做几件事情,但是却做得很好。正如其名字所暗示的,它能计算字段内的数字并得出这些数的总和、平均数、最大数或最小数。它还能让您从一个字段解析一个数字。该插件如此棒的灵活性得益于其能对来自任何类型的元素的数字进行求和。所以,如果传递给它一个包含 DIV、INPUT TEXT 和 SPAN 的元素数组,它将会找到这些字段内的数字,将其转变为数字并返回总和。这在某些情况下非常有用,并能省去尝试对每个数字运算函数进行错误检查的大量烦恼。                    
但是,关于此插件有一点需要注意,即它打破了标准的插件法则,即所有插件均返回此 jQuery 对象本身。在本文所讨论的其他插件以及我处理过的所有其他插件内,函数返回 jQuery 对象,这意味着它没有 “打破链”。而此插件则没有遵守。因为返回了数字,它打破了这条链,这样一来,在调用它之后,就不能再将这些 jQuery 函数串联在一起了。                    
让我们来看此插件的一个稍加扩展的例子。图 6 所示的小部件具有一组允许用户输入百分比的字段。这个小部件检查所有字段加起来是不是 100%,如果不是,就会将百分比的总和用红色显示以表明错误。在财务计划页面很容易遇到这类小部件,让您决定在哪方面增加投入。                    
图 6. 401k contribution 小部件清单 10. Calculation 插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Set up the table that contains the widget.
// Note that we add a class to it called
// "percentSimple", which will be used in the jQuery
// code, as well as adding a unique
// ID to the table "sortTable1".
// Also note that each text field that will have a percent
// typed into it has the
// class of "percent" added to it.
// Finally, note in the table footer a field called "percentTotal"
<p><table width=300 class="percentSimple" cellpadding=0 cellspacing=0 id=sortTable1>
<tbody>
  <tr><td class="left">
    S&P 500 Index
  </td>
  <td>
    <input type=text class="percent textfield"> %
  </td>
  </tr>
  .......
<tfoot>
  <tr><td>
    Total
  </td>
  <td>
    <span class=percentTotal></span> %
  </td>
  </tr>
</tfoot>
   
// take advantage of our previous plug-in and limit the percent fields
// to just numbers
$(".percent").numeric();

// capture any keyup events from all the "percent" fields
// when these occur, we are going to recalculate the total percent
$("table.percentSimple input.percent").keyup(function(){
    // figure out which table this occurred in (in case there's more than
    // 1 widget on a page
    var table = $(this).parents().filter("table.percentSimple");
    var ID = table.attr("id");
    // Find the sum of all the "percent" fields, by using
    // our calculation plug-in
    var sum = $("#" + ID + " input.percent").sum();
    // cache the span called "percentTotal"
    var totalField = $("#" + ID + " .percentTotal");
    // update the total in the "percentTotal" field
    totalField.html(sum);
    // if the sum != 100%, then we'll attach an error to it
    if (sum != 100 && sum != 0)
    {
       totalField.addClass("error");
    }
    else
    {
       totalField.removeClass("error");
    }
});




正如您所见,此插件的用途有限,但是它非常有效,而且若能正确使用还能节省很多时间。虽然我只包括了 max/min、average 和 sum 函数,我仍能想象得到有人如何雄心勃勃地想要组合成一个完整的电子表格插件,其中包含 Microsoft Excel                    应用程序内的所有函数。想象一下这个电子表格插件可以提供标准的 deviation、payment 以及 future value(因为我经常接触金融问题,所以         我对这些函数很熟悉)函数。此外,再想象一下此电子表格插件如何能被那些希望在其页面上提供基本的电子表格功能,或允许用户无需输入任何复杂公式就能创建各自类似电子表格页面的 Web 站点使用。
返回列表