2

使用这个插件: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

4 回答 4

2

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

if (typeof $(this).attr('id') == 'undefined') {...}
于 2009-10-11T16:37:49.663 回答
0

好的,罗杰 - 我发现这个是因为我面临着同样的问题。这是我暂时解决它的方法。我确实希望该插件在将来得到修复。我的感觉是,此时您不需要解决问题,但它可能会帮助其他人。显然,它依赖于 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
于 2009-11-16T18:11:10.283 回答
0

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

于 2011-12-09T04:17:08.400 回答
0

谢谢瑞恩!我将点击事件添加到对我有用的评级取消类

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
        }
      });
于 2010-06-01T09:37:50.220 回答