Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Yii2:动态获取字段的Id_Javascript_Php_Jquery_Html_Yii2 - Fatal编程技术网

Javascript Yii2:动态获取字段的Id

Javascript Yii2:动态获取字段的Id,javascript,php,jquery,html,yii2,Javascript,Php,Jquery,Html,Yii2,我正在使用下面的代码自动填充另一个表中的数据,这很好 <?php $this->registerJs("$('#otinstrumententry-0-instrument_name').on('change',function(){ $.ajax({ url: '".yii\helpers\Url::toRoute("ot-note/instrument")."', dataType: 'json', method: 'GET

我正在使用下面的代码自动填充另一个表中的数据,这很好

<?php
$this->registerJs("$('#otinstrumententry-0-instrument_name').on('change',function(){
    $.ajax({
        url: '".yii\helpers\Url::toRoute("ot-note/instrument")."',
        dataType: 'json',
        method: 'GET',
        data: {id: $(this).val(),
        },
        success: function (data, textStatus, jqXHR) {
            $('#otinstrumententry-0-instrument_code').val(data.instrument_code);
            },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('An error occured!');
            alert('Error in ajax request');
        }
    });

});"); 
?>
提前感谢您对此提出的建议或任何替代解决方案

编辑 最初,ajax根本没有为后续字段启动,而是在更改代码之后-
$this->registerJs($(文档).on('change','form control',function(event)


现在它正在发送数据,但我仍然坚持在当前字段中填充数据,因为它仍然没有选择动态添加的字段,而是在第一行中更改了数据。

添加所有字段css类。和
$('.className')。on('change'…
使用\yii\helpers\Html::getInputId('model,'attribute'))

公共静态方法

为指定的属性名称或表达式生成适当的输入ID

此方法将结果getInputName()转换为有效的输入ID。例如,如果getInputName()返回Post[content],此方法将返回Post内容

公共静态字符串|数组getInputId($model,$attribute)

例如:

<?= Html::getInputId($model, 'attribute') ?>

从道义上说:

<?= Html::getInputId($model, '['.$index.']attribute') ?>


Hi vitalik ur suggestion确实适用于
。on('chang'…
)但如何提取要填充的目标字段的ID,即
$('otinstrumententry-0-instrument\u code')
$('className')。on('change'…
仅适用于第一行,它不会在后续事件中触发。更改任何输入并显示
id
输入字段。Hi Vitalik-我不知道,但在后续行中,我的控制台在查看firebug时不会触发ajax事件。显示完成html
<?= Html::getInputId($model, '['.$index.']attribute') ?>