E
E
EVOSandru62015-10-05 14:34:57
JavaScript
EVOSandru6, 2015-10-05 14:34:57

How to call javascript function in Yii in Jquery zone?

Good afternoon,

There is a problem with calling js code.

There is this piece in the view:

Listing 1:



1. If I call pium(), it executes, but in turn does not see the tadam() function.

2. In the main theme file main.php, the scripts.js file is included in the footer:

<?php   Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/js/scripts.js');?>


Any alerts are executed in it.

it has a simple func() function :

$(document).ready(function()
{
  function func()
     {
        alert("hello");
     }
});


While in the current action in Listing 1 , this func() function is called .
And then a bugger, the function was not found, although if you open firebug, you can open the outputs of the included file and everything is written there)

3. The problems started when I wanted to put the search in the js file into the ajax function (As a result of my bydlocode, the function lies in the current view). There is an autocomplete search:

Fragment:
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete',
                    [
                        'name'=>'name',
                        'value'=>'',
                        'source'=>Yii::app()->createUrl('hotels/autocomplete'),
                        'options'=>
                            [
                                'showAnim'=>'fold',
                                'minLength'=>'3',
                                'select'=>'js:function( event, ui )
                                {
                                    $("#nameSearch").val( ui.item.label );
                                    $("#nameValue").val( ui.item.label );
                                    // $("#nameID").val( ui.item.id );

                                    // Поиск отеля
                                    // searchHotel();
                                    pium();
                                    return false;
                                }',
                            ],
                        'htmlOptions'=>
                            [
                                'onfocus' => 'js: this.value = null; $("#nameValue").val(null); $("#selectedvalue").val(null);',
                                'class' => 'input-xxlarge search-query',
                                'name' => 'Hotels[name]',
                                'placeholder' => "Введите первые 3 и более названия",
                                'id'=>'nameSearch'
                            ],
                    ]);

                ?>


If instead of pium() we insert alert() , $('*').hide() or the body of the function that I took out ( searchHotel ), then they work. pium() is also processed if it is not in the jquery zone , as in Listing 1 . Do not process function calls in the jquery zone . What could be the reason? Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-10-05
@EVOSandru6

$(document).ready(function()
{
  function func()
     {
        alert("hello");
     }
});

You wrapped the function in a closure, thus limiting its scope. Remove $(document).readyor do window.func = function () {}.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question