【问题标题】:JQuery JSON callback not triggered未触发 JQuery JSON 回调
【发布时间】:2014-09-27 12:16:27
【问题描述】:

我正在使用 Yii 框架并尝试在我的 gridview 上启用 CStarRating。在输出 HTML 源代码中,我有以下内容:

<tr class="odd">
  <td>Bob</td><td>
    <span class="rating" id="6">
    <input id="6_0" value="1" type="radio" name="6" />
    <input id="6_1" value="2" type="radio" name="6" />
    <input id="6_2" value="3" type="radio" name="6" />
    <input id="6_3" value="4" type="radio" name="6" />
    <input id="6_4" value="5" type="radio" name="6" />
    </span>
  </td>
</tr>
<tr class="even">
  <td>Peter</td>
    <span class="rating" id="9">
    <input id="9_0" value="1" type="radio" name="9" />
    <input id="9_1" value="2" type="radio" name="9" />
    <input id="9_2" value="3" type="radio" name="9" />
    <input id="9_3" value="4" type="radio" name="9" />
    <input id="9_4" value="5" type="radio" name="9" />
    </span>
  </td>
</tr>

在 HTML 正文中,我有以下内容:

<script type="text/javascript">

  jQuery(function($) {
    jQuery('#6 > input').rating({'callback':function(){
                url = "clients/update";
                jQuery.getJSON(url, {id_client: '6', val: $(this).val()},
                function() {
                   if (data.status !== "success"){
                        alert("error");
                   }});}});
    jQuery('#9 > input').rating({'callback':function(){
                url = "clients/update";
                jQuery.getJSON(url, {id_client: '9', val: $(this).val()},
                function() {
                   if (data.status !== "success"){
                        alert("error");
                   }});}});

  });
</script>

但是当我点击星星时,什么也没发生。我是 JQuery 和 JSON 的新手,所以任何帮助都将不胜感激。谢谢!

【问题讨论】:

  • 首先:请不要使用数字作为Id。
  • 谢谢,我已经在前面连接了一个字符串,但仍然没有任何反应。我想我可能需要一些用于boydy onLoad 的脚本,但我不确定:(
  • 在 click 函数中设置警报,看看是否触发了 click 事件。
  • 对不起,点击功能在哪里?
  • 对不起。我的意思是你的评级回调。他们被处决了吗?

标签: jquery json yii callback


【解决方案1】:

忘记 HTML 和 jQuery。使用 Yii 你有这条线(或类似的东西)调用评级明星:

$this->widget('CStarRating',array('name'=>'rating'));

如果您想在您的Rating Stars 上添加CallBack,您可以在选项数组中添加回调,如下所示:

$this->widget('CStarRating',array('name'=>'rating','callback'=>'myFunctionCallBack'));

当星星被点击(see this)时,这个回调将被执行。

Yii CStarRating 是一个基于 jQuery Star Rating 的小部件,你用 jQuery 扩展做的所有事情,你都可以用 Yii 小部件来做。因此,请发送look here 以获取有关扩展的更多信息。

如果您想了解有关该小部件的更多信息(我建议您这样做),请查看文档here

使用小部件的一些示例:

一些属性:

<?php
$this->widget('CStarRating',array(
            'name'=>'rating_1',
            'value'=>'4',
            'minRating'=>1,
            'maxRating'=>10,
            'starCount'=>10,
            ));
?>

使用 Ajax:

<?php
$this->widget('CStarRating',array(
    'name'=>'star_rating_ajax',
    'callback'=>'
        function(){
                $.ajax({
                    type: "POST",
                    url: "'.Yii::app()->createUrl('cstarrating/ajax').'",
                    data: "star_rating=" + $(this).val(),
                    success: function(data){
                                $("#mystar_voting").html(data);
                        }})}'
  ));
echo "<br/>";
echo "<div id='mystar_voting'></div>";
?>

更多例子可以看this article,对你有帮助。

【讨论】:

  • 非常感谢您花时间提供帮助。其实我的问题比较复杂。对于单个 CActiveRecord,这是真的,但我正在尝试启用对 CGridView 的投票。请参阅:yiiframework.com/forum/index.php/topic/…。我将不胜感激任何建议。再次感谢您!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 2020-06-24
  • 1970-01-01
  • 2011-09-30
  • 2012-05-04
  • 1970-01-01
  • 2018-12-23
相关资源
最近更新 更多