【问题标题】:Error on send an array to another function in jquery将数组发送到 jquery 中的另一个函数时出错
【发布时间】:2014-07-09 11:04:43
【问题描述】:

我在点击查看按钮时收到 Uncaught SyntaxError: Unexpected identifier 消息!?

我认为问题出在这部分:onClick="showModal('+value+');"

因为当我从 [onClick=""]、[showModal(value);] 中调用该函数时,它就起作用了!

怎么了?有什么想法吗?

谢谢。

JQUERY:

jQuery(document).ready(function($){

    divElement='';
    $.ajax({        
       type: "POST",
       url: "<?php echo Yii::app()->createUrl('itemtype/index');?>",
       data:{},
       dataType: "json",
       success: function(data) {

           $.each( data, function( i, value ) {
                    //showModal(value);
                    divElement+='<li class="itemtype-buttons">'+
                                '<a class="itemtype-button" href="#" onClick="showModal('+value+');" data-toggle="tooltip" data-original-title="View"><i class="fa fa-eye"></i></a>'+
                                '<a class="itemtype-button" href="#" data-toggle="tooltip" data-original-title="Edit"><i class="fa fa-edit"></i></a>'+
                                '<a class="itemtype-button" href="#" data-toggle="tooltip" data-original-title="Delete"><i class="fa fa-times"></i></a>'+
                                '<a class="itemtype-button" href="#" data-toggle="tooltip" data-original-title="List of '+value['name']+'"><i class="fa fa-bars"></i></a>'+
                                '</li>';
                });

            divElement+='</div>';                           
            $('#itemtypes').html(divElement);   

       }
    });

}); 

//itemtype Controller
public function actionIndex()
{
        $data = Itemtype::model()->findAll();
        echo CJSON::encode($data);

}
 function showModal(arr){

            alert(arr['name']);
}

提前致谢

【问题讨论】:

    标签: jquery json yii


    【解决方案1】:

    读取字符串拼接块外的name属性,然后使用,

    var name = value['name'];
    

    在字符串内部,

    '......title="List of '+ name +'">......'
    

    【讨论】:

    • 问题出在这部分:onClick="showModal('+value+');" no '......title="List of ' + name +'">......' 我需要在 showModal(arr) 函数中使用数组,alert(arr['name']);只是一个例子。
    猜你喜欢
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多