gpt4 book ai didi

javascript - 自动填充动态字段的 ajax 响应

转载 作者:行者123 更新时间:2023-11-28 07:32:00 24 4
gpt4 key购买 nike

我正在使用 ajax 请求自动填充,这对于静态字段工作得很好。例如:

$this->registerJs("$(document).delegate('.form-control','change',function(event){

$.ajax({
url: '".yii\helpers\Url::toRoute("ot-note/instrument1")."',
dataType: 'json',
method: 'GET',
data: {id: $(this).val(),
},
success: function (data, textStatus, jqXHR) {
$('#otinstrumententry-0-instrument_code').val(data.instrument_code);

},

beforeSend: function (xhr) {
alert('loading!');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('An error occured!');
alert('Error in ajax request');
}
});
});");

我的字段是动态添加的,对于动态添加的字段不起作用,代码如下:字段的 id 变得像#field-0-instrument_code#field-1-instrument_cdoe

除第一个字段外,Jquery 无法识别后续字段,这些字段是动态添加的。

如何解决这个问题。谢谢。

添加动态字段代码

<div id="instrument_entry">
<h3>Instruments Used</h3>
<?php $id = 0; ?>

<?php foreach ($otinstrumentModels as $otinstrument) { ?>

<div id="language" class="work-data-pad brdr-work marbtm10 row">
<div class="col-md-4">
<?= $form->field($otinstrument, '[' . $id . ']' . 'instrument_name')->DropDownList(ArrayHelper::map(\app\models\Instrument::find()->all(), 'id', 'instrument_name' ),
[ 'prompt' => 'Please Select' ])?>
</div>
<div class="col-md-2">
<?= $form->field($otinstrument, '[' . $id . ']' . 'instrument_code')->textInput(['maxlength' => 255,'class'=>'form-control']) ?>
</div>
<div class="col-md-1">
<?= $form->field($otinstrument, '[' . $id . ']' . 'hrs_time')->textInput(['maxlength' => 255])->label('Hrs-Time') ?>
</div>
<div class="col-md-2">
<?= $form->field($otinstrument, '[' . $id . ']' . 'total_charges')->textInput(['maxlength' => 255]) ?>
</div>
<?php ?>
<div style="margin-top: 30px;" class="col-md-3 <?php echo ($id < 1) ? 'dnone' : 'dblock'; ?>" id="divDelete" class="row-fluid">
<a class="ft11 btn-remove" onclick="deleteSection(this, 'instrument_entry');"><span class="marleft18">Remove</span></a>
</div>
</div>
<?php $id++; ?>
<?php } ?>
</div>

生成的 HTML

<div id="instrument_entry">
<h3>Instruments Used</h3>



<div id="language" class="work-data-pad brdr-work marbtm10 row">
<div class="col-md-4">
<div class="form-group field-otinstrumententry-0-instrument_name">
<label class="control-label" for="otinstrumententry-0-instrument_name">Instrument Name</label>
<select id="otinstrumententry-0-instrument_name" class="form-control" name="OtInstrumentEntry[0][instrument_name]">
<option value="">Please Select</option>
<option value="1">IMPLANTS(ORTHOPEDIC)</option>
<option value="2">O2 CHARGES PER HOUR</option>
</select>

<div class="help-block"></div>
</div>
</div>
<div class="col-md-2">
<div class="form-group field-otinstrumententry-0-instrument_code">
<label class="control-label" for="otinstrumententry-0-instrument_code">Instrument Code</label>
<input type="text" id="otinstrumententry-0-instrument_code" class="form-control" name="OtInstrumentEntry[0][instrument_code]" maxlength="255">

<div class="help-block"></div>
</div>
</div>
<div class="col-md-1">
<div class="form-group field-otinstrumententry-0-hrs_time">
<label class="control-label" for="otinstrumententry-0-hrs_time">Hrs-Time</label>
<input type="text" id="otinstrumententry-0-hrs_time" class="form-control" name="OtInstrumentEntry[0][hrs_time]" maxlength="255">

<div class="help-block"></div>
</div>
</div>
<div class="col-md-2">
<div class="form-group field-otinstrumententry-0-total_charges">
<label class="control-label" for="otinstrumententry-0-total_charges">Total Charges</label>
<input type="text" id="otinstrumententry-0-total_charges" class="form-control" name="OtInstrumentEntry[0][total_charges]" maxlength="255">

<div class="help-block"></div>
</div>
</div>
<div style="margin-top: 30px;" class="col-md-3 dnone" id="divDelete" class="row-fluid">
<a class="ft11 btn-remove" onclick="deleteSection(this, 'instrument_entry');"><span class="marleft18">Remove</span></a>
</div>
</div>

</div>

<div class="row">
<div class="col-md-12">
<button type="button" class="btn btn-primary sec-btn marbtm10" onclick="addNewSection('instrument_entry', 'OtInstrumentEntry')">+ Add Instrument</button>
</div>
</div>

<div class="row" style="margin-top: 20px;">
<div class="col-md-12">
<button type="submit" class="btn btn-success">Create</button> </div>
</div>

</form>

最佳答案

首先,你可以尝试这个:

$this->registerJs("$(document).delegate('.form-control','change',function(event){
sendAjax(this);
});");

function sendAjax(element)
{
$.ajax({
url: '".yii\helpers\Url::toRoute("ot-note/instrument1")."',
dataType: 'json',
method: 'GET',
data: {id: $(element).val()},
success: function (data, textStatus, jqXHR) {
$('#otinstrumententry-0-instrument_code').val(data.instrument_code);

},

beforeSend: function (xhr) {
alert('loading!');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('An error occured!');
alert('Error in ajax request');
}
});
}

现在您可以在新添加的元素中添加 onchange="sendAjax(this)" 。我认为这个解决方案可以解决您的问题。

关于javascript - 自动填充动态字段的 ajax 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29029851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com