我试图填充" num"在文本框中"年"和" console"被选中。数据库查询应该像 - 在年份='年份时从表中选择max(num)+1一样运行。和控制台=' someconsole'。 我的控制器操作看起来像 -
public function actionGetForNum($yearid , $consoleid)
{
$num = Bills::find()->select('(max(num) + 1) as num')->where(['bills_year'=>$yearid])->andWhere(['console'=>$consoleid])->asArray()->one();
echo Json::encode($num);
}
我使用select2小部件来选择年份和控制台。年份和控制台不相互依赖。
<?= $form->field($model, 'bills_year')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Year::find()->orderBy(['yid' => SORT_DESC,])->all(),'year_year','year_year'),
'language' => 'en',
'options' => ['placeholder' => 'Select Year', 'id' => 'yearid'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'console')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Console::find()->orderBy(['consoleid' => SORT_ASC,])->all(),'console','console'),
'language' => 'en',
'options' => ['placeholder' => 'Select Console','id' => 'consoleid'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
我尝试过的javascript代码是 -
<?php
/* start getting the num */
$script = <<< JS
$(function(){
$('#yearid').change(function(){
getNum();
});
$('#consoleid').change(function(){
getNum();
});
var yearid = $(this).val();
var consoleid = $(this).val();
var getNum = $.get('index.php?r=invoice/bills/get-for-num',{ yearid : yearid, consoleid : consoleid }, function(data){
alert(data);
var data = $.parseJSON(data);
$('#bills-num').attr('value',data.num);
});
});
JS;
$this->registerJs($script);
/* end getting the num */
?>
在此代码中我收到错误 - TypeError: getNum is not a function
这是错误屏幕 -
请建议如何纠正它。
更新 - 我已经更改了以下javascript代码 -
<?php
/* start getting the num */
$script = <<< JS
$(function(){
$('#yearid').change(function(){
getNum();
});
$('#consoleid').change(function(){
getNum();
});
var yearid = $(this).val();
var consoleid = $(this).val();
var getNum = function(){
var yearid = String($('#yearid').val());
var consoleid = String($('#consoleid').val());
$.get('index.php?r=invoice/bills/get-for-num',{ yearid : yearid, consoleid : consoleid }, function(data){
alert(data);
var data = $.parseJSON(data);
var getNum = data;
$('#bills-num').val(getNum);
});
} ;
});
JS;
$this->registerJs($script);
/* end getting the num */
?>
答案 0 :(得分:1)
$('#bills-num').val(getNum["num"]);