【问题标题】:jQuery Knob Release function additional parameterjQuery Knob Release 函数附加参数
【发布时间】:2013-12-31 10:57:56
【问题描述】:

我正在使用 jQuery Knob 插件https://github.com/aterrien/jQuery-Knob

现在,我得到了以下 jQuery 旋钮初始化

loop
   <input type="text" value="<?php echo $score; ?>" class="circle-rating" data-entryid="<?php the_ID(); ?>">
endloop



$(function() {
    $(".circle-rating").knob({
            'min':0,
            'max':10,
            'step':1,
            'width':40,
            'height':40,
            'fgColor':"#F59B00",
            'inputColor':"#F59B00",
            'displayPrevious': true,
            'release' : function (v) { 

                var entry_id = $(this).attr('data-entryid');

                jQuery.post("/path/to/file/update_library_score.php", {v : v, entry_id : entry_id}, function(data) {
                    console.log(entry_id);
                    jQuery('#notification-general').html(entry_id);

                });
            }

    });
});

主要问题是我在页面上有多个旋钮。这些旋钮实际上与 wordpress 帖子处于循环中。

不管怎样,每个旋钮都连接到一个ID,这个ID会随着你循环而改变。

现在为了能够更新分数,我需要从 release 函数获得的旋钮 value 和我只能在循环中获得的 post_id 两件事。那么如何才能将post_id 变量传递给这个函数呢?

通常我可以简单地添加button 或带有onclick="my_function(&lt;?php echo $post_id; ?&gt;) 的链接,但是我不能这样做。获取与此旋钮对应的 $id 并将其作为参数传递给释放函数的最佳方法是什么?

【问题讨论】:

  • 您可以通过data-*.circle-rating 元素使用自定义属性,并在您的发布功能中通过$(this).attr('data-*') 访问它。其中 * 可以是你想要的任何字符串
  • @Nouphal.M 我已经用你所说的(我认为)更新了我的问题,但它似乎不起作用。
  • entry_id 输出什么?签入控制台或提醒值和
  • @Nouphal.M 修复了语法错误并尝试了console.log(entry_id);,正如您在更新的问题代码中看到的,控制台返回undefined

标签: javascript php jquery wordpress jquery-knob


【解决方案1】:

在发布功能中试试这个

alert(this.$.attr('data-entryid'));

查看演示here

【讨论】:

  • 是的,似乎 this.$.attr('data-entryid') 有效,而不是 $(this).attr('data-entryid')
猜你喜欢
  • 2015-08-28
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 2023-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多