【问题标题】:Jquery star rating plugin and cancel rating buttonJquery星级评分插件和取消评分按钮
【发布时间】:2012-12-11 01:27:24
【问题描述】:

使用这个插件:http://www.fyneworks.com/jquery/star-rating/#tab-Testing

我有一个简单的回调函数,可以从单选按钮中获取 id:

<input type="radio" class="auto-submit-star {split:2}" id="myid" value="1" />

$('.auto-submit-star').rating({ 
  callback: function(value, link){ 
   alert($(this).attr('id'));
  } 
});

这工作正常,但如果用户点击取消按钮,那么它就无法读取它的 id。

在js中,我认为取消按钮是动态添加的:

control.cancel = $('<div class="rating-cancel"><a title="' + control.cancel + '">' + control.cancelValue + '</a></div>')

如果我像这样向它添加一个 id:

control.cancel = $('<div class="rating-cancel"><a id="someid" title="' + control.cancel + '">' + control.cancelValue + '</a></div>')

我如何读取 id?这将是未定义的。我可以设置一个类并使用 $('.myclass').attr('id') 但我将在一个页面上有多个评级,所以我需要类似于“this”的东西。或者取消按钮是否可以获取相应单选按钮的id?

【问题讨论】:

    标签: jquery plugins rating


    【解决方案1】:

    如果id 未定义,那么您知道他们单击了取消按钮。无需为它设置id

    if (typeof $(this).attr('id') == 'undefined') {...}
    

    【讨论】:

    • 我需要获取 id 的原因是将其传递给我的数据库。每页也会有不止一个评分。因此,例如,如果我在一页上有 3 个评分,并且用户在第 2 个评分上单击了取消按钮,我需要知道它在第 2 个评分中,并且我需要将该特定 ID 插入到我的一个列。希望你明白我的意思。
    【解决方案2】:

    好的,罗杰 - 我发现这个是因为我面临同样的确切问题。这是我暂时解决它的方法。我确实希望该插件在将来得到修复。我的感觉是,此时您不需要解决问题,但它可能会帮助其他人。显然,它依赖于 DOM 元素的结构,因此不是一个非常优雅的解决方案。

    //RYAN OBEROI: A big hack, since the cancel callback does not have the appropriate information
    name = $(this).parent().next().attr('name')
    
    // click callback, as requested here: http://plugins.jquery.com/node/1655
    if(control.callback) control.callback.apply(input[0], [input.val(), $('a', control.current)[0], name]);// callback event
    

    【讨论】:

      【解决方案3】:

      感谢瑞恩!我将点击事件添加到对我有用的 rating-cancel 类

      jQuery(".rating-cancel").click(function() {
              var name = jQuery(this).parent().next().attr('name');
              if (name != "") {
                 //added the custom code here to handle rating cancel event
              }
            });
      

      【讨论】:

        【解决方案4】:

        这是插件中的一个错误。 有一个修复here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多