Yii - js功能不起作用

时间:2015-08-01 20:20:23

标签: javascript jquery yii

我尝试创建searchMap()函数,并在搜索按钮点击事件中调用它,但它不起作用并给我以下错误:

Uncaught ReferenceError: searchMap is not defined

我无法解释代码的问题

这是视图文件的代码

    <?php
/* @var $this EventListController */
/* @var $model EventList */
/* @var $form CActiveForm */
?>
<script  type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<?php  $baseUrl = Yii::app()->baseUrl;  $cs = Yii::app()->getClientScript(); $cs->registerScriptFile($baseUrl.'/js/gmaps.js',CClientScript::POS_END ); ?>

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'event-list-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
     'enableAjaxValidation'=>true,   
        'htmlOptions' => array('enctype' => 'multipart/form-data'),


)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'EVENT_DESC'); ?>
        <?php echo $form->textArea($model,'EVENT_DESC',array('rows'=>10,'cols'=>61)); ?>
        <?php echo $form->error($model,'EVENT_DESC'); ?>
    </div>
<div class="row">
    <?php echo $form->labelEx($model,'EVENT_LOCATION'); ?>
    <?php echo $form->textField($model,'EVENT_LOCATION',array('size'=>60,'maxlength'=>255)); ?>
    <?php echo $form->error($model,'EVENT_LOCATION'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'LAT'); ?>
    <?php echo $form->textField($model,'LAT',array('size'=>60,'maxlength'=>255)); ?>
    <?php echo $form->error($model,'LAT'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'LNG'); ?>
    <?php echo $form->textField($model,'LNG',array('size'=>60,'maxlength'=>255)); ?>
    <?php echo $form->error($model,'LNG'); ?>
</div>


          <div class="row">
        <label for="address">Address:</label>
        <input type="text" id="address" name="address" />
         <input type="button" class="btn" value="Search" onclick="searchMap();"/>
      </div>
<div id="map">
</div>

<div class="row">
    <?php echo $form->labelEx($model,'PRICE_ID'); ?>
    <?php //echo $form->dropDownList($model,'PRICE_ID'); ?>
    <?php echo CHtml::activeDropDownList($model,'PRICE_ID', $model->getPriceList(),array('prompt' => 'Select Price Range')); ?>
    <?php echo $form->error($model,'PRICE_ID'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'EVENT_PRIVACY'); ?>
    <?php echo $form->dropDownList($model,'EVENT_PRIVACY',array('Public'=>'Select Event Privacy','Public'=>'Public','Private'=>'Private')); ?>
    <?php echo $form->error($model,'EVENT_PRIVACY'); ?>
</div>



<div class="row">
        <?php echo $form->labelEx($model,'START_DATE'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'START_DATE',
                 'value'=>$model->START_DATE,
//additional javascript options for the date picker plugin
'options'=>array(
'dateFormat'=>'yy-mm-dd',
'showAnim'=>'fold',
                        'debug'=>true,
'datepickerOptions'=>array('changeMonth'=>true, 'changeYear'=>true),
),
'htmlOptions'=>array('style'=>'height:20px;'),
)); ?>
        <?php echo $form->error($model,'START_DATE'); ?>

    </div>



    <div class="row">
            <?php echo $form->labelEx($model,'END_DATE'); ?>
    <?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'model'=>$model,
    'attribute'=>'END_DATE',
                     'value'=>$model->END_DATE,
    //additional javascript options for the date picker plugin
    'options'=>array(
    'dateFormat'=>'yy-mm-dd',
    'showAnim'=>'fold',
                            'debug'=>true,
    'datepickerOptions'=>array('changeMonth'=>true, 'changeYear'=>true),
    ),
    'htmlOptions'=>array('style'=>'height:20px;'),
    )); ?>      <?php echo $form->error($model,'END_DATE'); ?>
        </div>

        <div class="row">


                <?php if($model->EVENT_IMAGE != null ){
                        echo CHtml::image(Yii::app()->request->baseUrl.'/images/event/'.$model->EVENT_IMAGE,"EVENT_IMAGE",array("width"=>200));  ?>
                <?php echo $form->labelEx($model,'EVENT_IMAGE'); ?>
                <?php echo $form->fileField($model,'EVENT_IMAGE',array('size'=>60,'maxlength'=>255)); 
                } else { ?>
                <?php echo $form->labelEx($model,'EVENT_IMAGE'); ?>
                <?php echo $form->fileField($model,'EVENT_IMAGE',array('size'=>60,'maxlength'=>255)); ?>
                <?php //echo $form->error($model,'EVENT_IMAGE'); ?>
                <?php }?>
        </div>

        <div class="row">
            <?php echo $form->labelEx($model,'IS_ACTIVE'); ?>
            <?php echo $form->checkBox($model,'IS_ACTIVE'); ?>
            <?php echo $form->error($model,'IS_ACTIVE'); ?>
        </div>
        <div class="row buttons">
            <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
        </div>
        <?php Yii::app()->clientScript->registerScript("map","

        function searchMap(){
        alert($('#address').val());

        }
        var lat;
        var lng;
        var m = new GMaps({
          div: '#map',
          lat: 39.727172,
          lng: -101.202109,
          height: '350px',
          zoom: 5,
          center: new google.maps.LatLng(39.727172, -101.202109),
          click: function(e) {
              m.removeMarkers();
               lat = e.latLng.lat();
               lng = e.latLng.lng();
            //   alert(lat +','+lng);
               $('#EventList_LAT').val(lat);
               $('#EventList_LNG').val(lng);    
                var marker = m.addMarker({
                        lat: lat,
                        lng: lng,
                 });

                    var latlng = new google.maps.LatLng(lat, lng);
                GMaps.geocode({
                  location : latlng ,
                  callback: function(results, status) {
                   if (results[1]) {
                      //m.setZoom(11);
                      m.addMarker({
                            lat: lat,
                            lng: lng,
                            infoWindow: {content: results[1].formatted_address},
                            });
                        $('#EventList_EVENT_LOCATION').val(results[1].formatted_address);   
                        } else {
                            window.alert('No results found');
                            }
                    }
            });
          },

        });



        "); ?>


    <?php $this->endWidget(); ?>

    </div><!-- form -->

1 个答案:

答案 0 :(得分:1)

您没有向CClientScript::registerScript提供$position参数。默认情况下,Yii将JavaScript代码的加载推迟到CClientScript::POS_READY,其中:

  

脚本插入到jQuery的ready函数中。

您的onclick处理程序正在全局范围内查找命名函数;而Yii已经将你的脚本置于jQuery的准备状态(可能在匿名函数中)。

您有两种选择:

  1. $position设为CClientScript::POS_HEADCClientScript::POS_BEGIN。 (注意:这不是最理想的,因为它可以在加载jQuery之前触发。也就是说,$尚未定义。)
  2. 删除onclick处理程序并使用jQuery绑定事件,例如:

    id元素添加input属性:

    <input type="button" class="btn" value="Search" id="search_map" />
    

    使用jQuery的on

    $('#search_map').on('click', function(){
        alert($('#address').val());
    });