gpt4 book ai didi

javascript - 如何在动态创建的行中使用下拉列表中最接近文本框的设置值

转载 作者:行者123 更新时间:2023-11-27 22:45:29 25 4
gpt4 key购买 nike

当用户从下拉列表中选择 channel 名称时,我想将其 channel 类型设置为动态创建的行中的文本框。我在 addRow 事件上创建多行。工作正常

这是html代码

 <table id="dataTable"  border="1" class="table table-striped table-bordered" >


<tr>
<td><INPUT type="checkbox" name="chk[]" data-parsley-errors-container="#checkbox-errors" /></td>
<td>
<SELECT name="channel_name[]" onclick ="get_type(this)"; class='channelname'>
<option value="">Select...</option>
<?php foreach($channel_list as $row) {
$channelid = $row['channelid'];
$channelname = $row['channelname'];?>
<OPTION value='<?php echo $channelid ?>'><?php echo $channelname?></OPTION>

<?php } ?>
</SELECT>
</td>
<td><INPUT type="text" name="type[]" class="channeltype"/></td>
<td><INPUT type="text" name="partner_share[]"/></td>
</tr>

</table>

Javascript代码:

function get_type()
{
$(".channelname").live("change", function() {

var channel_id = $(this).find("option:selected").attr("value");
$.ajax({
type: "POST",
url: '<?php echo base_url(); ?>index.php/partner/get_channel_type',
data: 'channelid='+channel_id,
async: false
}).done(function( data1 ) {

if(data1){
alert(data1);
//$(this).closest('tr').children('td.type').val(data1);
//$(this).closest('tr').find('.type').val(data1);
$(this).closest("tr").find('input[name="channeltype[]"]').val(data1);
//$(this).closest("tr").find('input[name="usage_unit[]"]').val(ui.item.usage_unit);


}else{
alert("Channel type is not defined");

}


});
});
}

最佳答案

没有名为 channeltype[] 的输入行:

$(this).closest("tr").find('input[name="channeltype[]"]').val(data1);

应该是:

$(this).closest("tr").find('input[name="type[]"]').val(data1);

因为 channel 类型输入名称是type[],如下所示:

<td><INPUT type="text" name="type[]" class="channeltype"/></td>

您必须保存jquery实例$(this),因为它在成功回调中会有所不同:

 ...
var channel_id = $(this).find("option:selected").attr("value");
var _this = $(this); //Save current object

$.ajax({
...

然后在回调中使用它:

...
_this.closest("tr").find('input[name="type[]"]').val(data1);
...

希望这有帮助。

关于javascript - 如何在动态创建的行中使用下拉列表中最接近文本框的设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38432539/

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