我有一個名爲employee_name
的字段,根據此字段值,我想自動填充另一個字段employee_id
。我搜索了,我發現this答案,並嘗試在我的表單上實現這一點,但我得到Error in ajax request
。在我的表格jQuery代碼是yii2根據另一個字段自動填充相關字段
$('#emp').focusout(function() {
empName = this.value;
if (empName != '' || empName != null) {
$('#depcustomer-employee_name').val(empName);
}
$.ajax({
url: '".yii\helpers\Url::toRoute("deposit/employeeid")."',
dataType: 'json',
method: 'GET',
data: {name: $(this).val()},
success: function (data, textStatus, jqXHR) {
$('#depcustomer-employee_id').val(data.id);
},
beforeSend: function (xhr) {
alert('loading!');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('An error occured!');
alert('Error in ajax request');
}
});
});
我的控制器名稱爲Deposit
和我的控制器代碼是
public function actionEmployeeid($name){
$model= app\modules\settings\models\DepEmployee::findOne(['employee_name'=>$name]);
return \yii\helpers\Json::encode([
'id'=>$model->employee_id
]);
可能是什麼原因可能是我的Ajax代碼是不工作?
我的表格很大。這裏的員工字段項的一部分
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'employee_id')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-md-6">
<label for='emp'>Employee Name</label>
<?= Html::activeHiddenInput($model, 'employee_name')?>
<?php
echo AutoComplete::widget([
'name' => 'employee_name',
'id' => 'emp',
'clientOptions' => [
'source' => $dataEmp,
'autoFill'=>true,
'minLength'=>'2',
'select' => new JsExpression("function(event, ui) {
$('#depcustomer-name').val(ui.item.id);
}")
],
]);
?>
</div>
</div>
你怎麼能指望jQuery來知道這一點:'警予\傭工\地址:: toRoute( 「存款/僱員」)' ?請發佈您的表單以獲得更多幫助。 – stfsngue
@stfsngue所以,我應該用像'http://localhost/testingDeposit/backend/web/index.php?r = deposit%2Fdeposit%2Femployeeid'這樣的絕對網址來代替它 – gojiraki
試試這個答案。 – stfsngue