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

发布于 2024-08-06 22:06:08 字数 982 浏览 2 评论 0原文

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

这工作正常,但是如果用户单击取消按钮,则它无法读取它。

在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?

Using this plugin: http://www.fyneworks.com/jquery/star-rating/#tab-Testing

I have a simple callback function that picks up the id from the radio buttons:

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

This works fine, but if the user clicks on the cancel button, then it can't read the id of it.

In the js, I think the cancel button is added dynamically with:

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

If I add an id to it like this:

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

How could I read the id? This would be undefined. I can set a class and use $('.myclass').attr('id') but I will have multiple ratings on one page so I'll need something similar to "this". Or is it possible for the cancel button to pick up the id of the corresponding radio buttons?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

节枝 2024-08-13 22:06:08

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

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

If the id is undefined, then you know they clicked the cancel button. No need to set an id for it.

if (typeof $(this).attr('id') == 'undefined') {...}
慢慢从新开始 2024-08-13 22:06:08

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

Ok Roger - I found this because I was facing the same EXACT issue. Here is how I solved it for the time being. I do hope the plugin gets fixed in the future. My sense is that you don't need the issue resolved at this point, but it may help out other folks. Obviously, it relies on structure of the DOM elements, hence not a very elegant solution.

//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
蓝色星空 2024-08-13 22:06:08

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

jQuery(". rating-cancel").click(function() {
        var name = jQuery(this).parent().next().attr('name');
        如果(名称!=“”){
           //在这里添加自定义代码来处理评级取消事件
        }
      });

Thanks Ryan! I added the click event to rating-cancel class which worked for me

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
        }
      });
野生奥特曼 2024-08-13 22:06:08

这是插件中的一个错误。
此处已修复。

This is a bug in the plugin.
There's a fix here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文