本文介绍了下载JavaScript onload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉菜单,它有一个onchange功能。一旦该功能被执行,它会改变另一个下拉菜单。
我需要这样做才能执行脚本onload。



第一个下拉列表:

  echo $ form->字段($ model,'company_id') - > dropDownList($ items_company,['prompt'=>'选择公司','style'=>' width = 400px;','onchange'=>'
$ .post(index.php?r = project / lists& id ='。'+ $(this).val(),function数据){
$(select#project-client).html(data);
console.log(On change);
console.log(data);
});

',]) - > label('Company');

第二个下拉列表:

  echo'< label class =control-label> Company Client< / label>'; 
echo Select2 :: widget([
'model'=> $ model,
'attribute'=>'client',
'theme'=> Select2: :THEME_BOOTSTRAP,
'options'=> ['label'=>'Client',
'multiple'=> true,'style'=>'width:400px;',
'pluginOptions'=> [
'disabled'=> false,
],
]);

这是我试过的:

<$ p $($#$> $($)$($)$($)$($)$($)$($) index.php?r = project / lists& id ='。'+ currentProjectCompany,function(data){
$(select#project-client)。html(data);
console。 log(Company ID:);
console.log(currentProjectCompany);
console.log(Clients);
console.log(data);
}) ;
});


解决方案

将onchange代码移入它自己的函数),然后在ready()函数中执行该函数。

这样它就会触发onchange和onload。

I have a dropdown menu, that has a onchange function. Once the function is executed it changes another dropdown menu.I need to make it so it executes the script onload.

1st dropdown:

echo $form->field($model, 'company_id')->dropDownList($items_company, ['prompt' => 'Select Company', 'style' => 'width:400px;', 'onchange' => '
            $.post("index.php?r=project/lists&id=' . '"+$(this).val(), function( data ) {
            $( "select#project-client" ).html( data );
            console.log("On change");
            console.log(data);
            });

        ',])->label('Company');

2nd dropdown:

echo '<label class="control-label">Company Client</label>';
echo Select2::widget([
'model' => $model,
'attribute' => 'client',
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [ 'label' => 'Client',
    'multiple' => true, 'style' => 'width:400px;', 'overwriteInitial' => true],
'pluginOptions' => [
    'disabled' => false,
],
]);

This is what I tried:

$(document).ready(function () {
    var currentProjectCompany = $('#project-company_id').val();
    $.post("index.php?r=project/lists&id=' . '" + currentProjectCompany, function (data) {
        $("select#project-client").html(data);
        console.log("Company ID:");
        console.log(currentProjectCompany);
        console.log("Clients");
        console.log(data);
    });
});
解决方案

Move the onchange code into its own function (it should be there anyway), and execute that function in the ready() function.

That way it will fire both onchange and onload.

这篇关于下载JavaScript onload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:20