Jquery星级评级插件和取消评级按钮

时间:2009-10-11 16:30:22

标签: jquery plugins rating

使用此插件: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>')

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

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

Ok Roger - 我发现这是因为我遇到了同样的问题。以下是我暂时解决的问题。我希望将来能够修复该插件。我的感觉是你现在不需要解决这个问题,但它可能有助于其他人。显然,它依赖于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

答案 2 :(得分:0)

谢谢Ryan!我将点击事件添加到为我工作的评级取消课程

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
        }
      });

答案 3 :(得分:0)

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