【问题标题】:Failure invoking a modal window through jquery in Yii在 Yii 中通过 jquery 调用模式窗口失败
【发布时间】:2013-10-11 13:51:30
【问题描述】:

我尝试将模态窗口添加到我的 yii 应用程序。 为此,我使用框架中的 jquery。 然而,它不适用于框架中的 jquery (1.8.3):framework/web/js/source
错误是:
Uncaught TypeError: Object [object Object] has no method 'modal'
在这一行:

$('#dataModal').modal().css({
            width: 'auto',
            height: 'auto',
    });

(请参阅下面的完整代码)。
当我将 modal() 交换为 dialog() 时也会发生同样的情况。

然而,当我尝试通过 googleapis 通过将其注册为客户端脚本来上传最新的 jquery (1.10.2) 时,它可以工作(虽然每次视图调用只有一次): config/main.php:

'components'=>array(
...
 'clientScript'=>array(
    'packages'=>array(
        'jquery'=>array(
            'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jquery/1/',
            'js'=>array('jquery.min.js'),
        ),
    ),
 ),

并在视图中注册:

Yii::app()->clientScript->registerCoreScript('jquery');

该案例的完整代码在这里:

<!-- modal window -->      
<?php $this->beginWidget('bootstrap.widgets.TbModal', 
    array('id'=>'dataModal')); ?>
    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
        <h4><?=Yii::t("ui", "Выберите номенклатуру")?></h4>
        <!--?php $this->widget('bootstrap.widgets.TbButton', array(
            'label'=>Yii::t("ui", "Справочник номенклатуры"),
            'url'=>$this->createUrl('assortment/index'),            
            'htmlOptions'=>array('data-dismiss'=>'modal', 'class' => 'btn btn-medium btn-primary', /*'style'=>'float:right; clear:both;'*/),
        )); ?-->
    </div>
    <div class="modal-body"></div>
    <div class="modal-footer">
    <?php $this->widget('bootstrap.widgets.TbButton', array(
            'label'=>Yii::t("ui", "Справочник номенклатуры"),
            'url'=>Yii::app()->createUrl('assortment/index'),           
            'htmlOptions'=>array(/*'data-dismiss'=>'modal',*/ 'class' => 'btn btn-medium btn-primary', 'style'=>'float:left; /*clear:both;*/'),
        )); 
        $this->widget('bootstrap.widgets.TbButton', array(
            'label'=>Yii::t("ui", "Отмена"),
            'url'=>'#',
            'htmlOptions'=>array('data-dismiss'=>'modal'),
        )); ?>
    </div>
<?php $this->endWidget(); ?>
<!-- modal end --> 
 ...
<script type="text/javascript">
// this function calls the modal thru AJAX
$('#data-select-btn').click(function(){
   var buttn = this; 
   // $(buttn).button('loading'); 
    $.ajax({
      url: "<?php echo $this->createAbsoluteUrl('order/loadData') ?>", /*LoadDataCheckBox*/
      cache: false,
      data: "type="+$("#Order_type").val(),
      success: function(html){
        $(".modal-body").html(html);       
        //$(buttn).button('reset');
        $('#dataModal').modal().css({
            width: 'auto',
            height: 'auto',
            'margin-top': '-50px',
            'margin-left': function () {
                return -($(this).width() / 2) - 80;
            },
        });
      }          
    });
})
</script>

对于 Jquery-ui,这些也包括在内:

<script type="text/javascript" src="/application/assets/6d25656/jui/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="/application/assets/6d25656/jui/js/jquery-ui-i18n.min.js"></script>

更新

就 Yii 引导扩展我确实使用它,并且在 bootstrap.js 中有模态类定义和插件定义。 此文件在 jquery 之后加载,但在 jquery-ui 之前,顺序重要吗?

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
...
<script type="text/javascript" src="/application/assets/7d883f12/js/bootstrap.js"></script>

视图文件结束:

<script type="text/javascript" src="/application/assets/6d25656/jui/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="/application/assets/6d25656/jui/js/jquery-ui-i18n.min.js"></script>

【问题讨论】:

  • 要使用 .modal 或 .dialog,还必须包含对 jqueryUI 脚本的引用
  • jqueru-ui 也包括在内(见底部更新)。
  • 我看到你正在使用 Yii Bootstrap 扩展,你应该在那里寻找缺少的 modal() 函数。 jQueryUi 只有一个 dialog() 函数。
  • @davey,我如何在引导程序中找到缺失的 modal() 函数?
  • 我看了看,bootstrap.min.js 是否包含在您的页面中的某个地方? (虽然也没有 model() 函数)

标签: jquery jquery-ui yii modal-dialog yii-booster


【解决方案1】:

试试这个...在order/loadData 中确保您的renderPartial 没有第二次加载脚本。

$this->renderPartial('view_partial', array(
    'model' => $model,
), false, false);

http://www.yiiframework.com/doc/api/1.1/CController#renderPartial-detail

【讨论】:

  • 实际上模态窗口确实加载了一些需要更多脚本的表格内容。所以有: $this->renderPartial('view_partial', array( 'model' => $model, ), false, true);
【解决方案2】:

顺序很重要。

你应该先加载jquery,然后加载jquery-ui,thw bootsttrap.js

尝试在 registerScript 中指明位置:

Yii::app()->clientScript->registerCoreScript('jquery', CClientScript::POS_HEAD);

更多信息在这里:http://www.yiiframework.com/doc/api/1.1/CClientScript#registerScriptFile-detail

你是如何包含 jQuery UI 的?

===编辑=== 尝试使用以下命令更改 config/main 中脚本的顺序:

'components'=>array(
...
    'clientScript' => array(
        'coreScriptPosition' => CClientScript::POS_END,
    )
),

如果没有任何效果,只需像这样禁用自动加载:

'clientScript' => array(
    'scriptMap'=>array(
        'jquery.js'=>false,
        'jquery.min.js'=>false
    ),
),

然后像过去一样加载 ;)

<script src="<?php echo $this->baseURL ?>/js/jquery.min.js"></script>

【讨论】:

  • jQuery-ui 和 jquery 会被 Yii 自动加载(所有的 ajax 调用都由它们执行)。 Bootstrap.js 也应该是自动加载的,因为我已经安装了 Bootstrap ext。
  • Yii 首先加载(在 head 部分)jquery.js,然后在右下角 before
猜你喜欢
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 2021-06-10
  • 2015-07-17
  • 2016-04-01
  • 1970-01-01
相关资源
最近更新 更多