【问题标题】:How do I build a "stoplight" state indicator button using jQuery?如何使用 jQuery 构建“红绿灯”状态指示器按钮?
【发布时间】:2010-10-06 18:54:55
【问题描述】:

作为我的第一个 jQuery 项目,我正在尝试编写一个“红绿灯”状态指示器,循环显示“关闭”、“绿色”、“黄色”和“红色”圆圈图像并发布信息以便我可以记录它在数据库中。我目前正在尝试使用 toggle() 函数,它可以工作,只是我无法将值“初始化”为预先存在的数据库状态。

这是我所拥有的:

$('.stoplight').toggle(
    function () {
       $.post('post.aspx', { recordid: <%= this.Id %>, lightid: this.id, value: 'yellow' });
       $(this).attr('src', '/Images/Stoplights/yellowStatus.gif');
    },
    function () {
       $.post('post.aspx', { recordid: <%= this.Id %>, lightid: this.id, value: 'red' });
       $(this).attr('src', '/Images/Stoplights/redStatus.gif');
    },
    // ... and so forth for "off" and "green" settings

那么 - 我如何在页面加载期间以某种方式设置切换的起始状态,或者,是否有另一种方法可以更好地实现这一点?

【问题讨论】:

  • @Tormod:只是为了表明我正在构建的平台——我不认为这很重要,但我认为回答者可能需要更多地了解我的平台,我使用了几个微不足道的 ASP .NET 标签

标签: c# jquery asp.net-ajax


【解决方案1】:

这是切换各种灯光的另一种方式。我用的是div而不是图片,但是原理是一样的。在此处查看实际操作:http://jsfiddle.net/vy7wX/3/

$('.stoplight').click(function() {
    var lights = ['off','green','yellow','red'];

    // find current status
    var status = $(this).attr('class').replace('stoplight ','');

    // get current position in array
    var position = $.inArray(status, lights);

    // get new position
    var position_new;

    // -1 because array index starts with 0
    if (position == lights.length-1) {
      // if it's at the end, go to the beginning
      position_new = 0;  
    } else {
      // else take the next status in the array
      position_new = position+1;
    }

    // get the new status, based on position
    var status_new = lights[position_new];

    // switch classes
    $(this).toggleClass(status+' '+status_new);
});​

在您的情况下,您可以做两件事:检查当前图像 src 并检索红绿灯名称或向图像添加一个类,以便您可以直接检索名称。后者可能更容易。

例子:

&lt;img class="stoplight red" src="/Images/Stoplights/redStatus.gif" alt="red light" /&gt;

以及更改图片src和发布状态的JS:

$('.stoplight').click(function() {
    var lights = ['off','green','yellow','red'];

    // find current status
    var status = $(this).attr('class').replace('stoplight ','');

    // get current position in array
    var position = $.inArray(status, lights);

    // get new position
    var position_new;

    // -1 because array index starts with 0
    if (position == lights.length-1) {
      // if it's at the end, go to the beginning
      position_new = 0;  
    } else {
      // else take the next status in the array
      position_new = position+1;
    }

    // get the new status, based on position
    var status_new = lights[position_new];

    // update image classnames and src
    $(this)
      .toggleClass(status+' '+status_new);
      .attr('src', '/Images/Stoplights/'+status_new+'Status.gif');

    // update database
    $.post('post.aspx', { recordid: <%= this.Id %>, lightid: this.id, value: status_new });
});​

使用它,灯光的起始位置是什么并不重要,您可以根据数据库中的当前状态简单地添加正确的类作为起始位置。

编辑
至于 jQuery 1.3.2。 documentation 表示 .toggleClass 方法是在 1.3 中添加的,它可以同时切换多个类。在网上的其他地方,我发现 1.3 只能处理 one 类,所以我假设该方法本身是在 1.3 中添加的,但它在 1.4 中获得了扩展功能,该功能没有在该页面上记录。

不管怎样,没问题。就像你说的,你可以简单地分别切换这两个类。但是,如果您需要使用该方法两次,则可以改用 removeClass 和 addClass,以便脚本更好地阅读。上一部分的课程更新我也忘记了,所以最后会是这样的:

// update image classnames and src
$(this)
  .removeClass(status)
  .addClass(status_new)
  .attr('src', '/Images/Stoplights/'+status_new+'Status.gif');

// update database
$.post('post.aspx', { recordid: <%= this.Id %>, lightid: this.id, value: status_new

另外,为了减少代码行数,position_new 变量可以这样定义:

// get new position
var position_new = position == lights.length-1 ? 0 : position+1;

【讨论】:

  • @Alex:这似乎在 jQuery 1.4.2 下工作,但不是 1.3.2,我很遗憾需要在这里使用它。 (我在 jsFiddle 上试过,在切换 jQuery 版本后,它停止了正常工作。它切换了一次,然后停止切换。)你知道发生了什么变化吗?
  • @Alex:如果我调用 toggleClass 两次,一次用于状态,一次用于 newStatus,它在 1.3.2 下工作。但它不适用于您提供的形式。很奇怪,因为我在任何地方都找不到这个问题。不过谢谢!如果您在答案中做出更改,我会将其标记为已接受。
猜你喜欢
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多