gpt4 book ai didi

javascript - 在自动完成功能中断的表单中添加行

转载 作者:行者123 更新时间:2023-12-03 04:58:29 25 4
gpt4 key购买 nike

在一个表单中,我有 2 个输入。在第一个输入中,我使用 datalist我通过 JSON 加载它当第一个输入更改时,第二个输入会自动完成。到这里一切正常!

然后我添加了一个添加按钮,在其中添加相同的行。问题是因为我使用 id选择输入,当我添加新行时,我有相同的 id.. 所以自动完成不起作用..

我该如何解决这个问题?这里是jssFiddle .

counter = 0;
var dataList = document.getElementById('products');
var jsonOptions = [{
"product": "11111",
"description": "description 1"
}, {
"product": "22222",
"description": "description 2"
}, {
"product": "33333",
"description": "description 3"
}];

jsonOptions.forEach(function(item) {

var option = document.createElement('option');

option.value = item.product;
option.text = item.description;

dataList.appendChild(option);


$(function() {
$('#product').on('change keyup', function() {

var i = this.value;
var description = "";
var productsInBox = 0;

jsonOptions.forEach(function(a) {
if (a.product == i) {
description = a.description;
productsInBox = a.productsInBox;
}
});
$('#description').val(description);


});
});

});



$('#form1')
// Add button click handler
.on('click', '.addButtonDED', function() {
counter++;
var $template = $('#addLineInDed'),
$clone = $template
.clone()
.removeClass('hide')
.removeAttr('id')
.attr('data-index', counter)
.insertBefore($template);

// Update the name attributes
$clone
.find('[name="product"]').attr('name', 'product-' + counter).end()
.find('[name="description"]').attr('name', 'description-' + counter).end()


})

// Remove button click handler
.on('click', '.removeButtonDED', function() {
var $row = $(this).parents('.form-group'),
index = $row.attr('data-index');
counter--;
// Remove element containing the fields

$row.remove();
});
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="form1" method="post" class="form-horizontal" role="form">
<fieldset>


<div class="form-group">
<div class="col-xs-2">
<input type="text" id="product" list="products" class="form-control" name="product-0" />
<datalist id="products"></datalist>
</div>

<div class="col-xs-4">
<input id="description" type="text" class="form-control" name="description-0" />
</div>



<div class="col-xs-1">
<button type="button" class="btn btn-default addButtonDED"><i class="fa fa-plus"></i></button>
</div>
</div>


<div id="addLineInDed" class="form-group hide">
<div class="form-group">
<div class="col-xs-2">
<input type="text" id="product" list="products" class="form-control" name="product-0" />
<datalist id="products"></datalist>
</div>

<div class="col-xs-4">
<input id="description" type="text" class="form-control" name="description-0" />
</div>


<div class="col-xs-1">
<button type="button" class="btn btn-default removeButtonDED"><i class="fa fa-minus"></i></button>
</div>
</div>
</div>


<div class="col-md-10 ">
<button type="submit" name="formAction" value="next" class="btn btn-primary">Submit</button>
</div>

</fieldset>
</form>

最佳答案

使用类,将您的选择重命名为数组值:

counter = 0;
var dataList = $('.products');
var jsonOptions = [{
"product": "11111",
"description": "description 1"
}, {
"product": "22222",
"description": "description 2"
}, {
"product": "33333",
"description": "description 3"
}];

jsonOptions.forEach(function(item) {

var option = '<option value="' + item.product + '">' + item.description + '</option>';

dataList.append(option);
});

$(function() {
$('body').on('input', '.product,.products', function() {

var i = this.value;
var description = "";
var productsInBox = 0;

jsonOptions.forEach(function(a) {
if (a.product == i) {
description = a.description;
productsInBox = a.productsInBox;
}
});
$(this).closest('.form-group').find('.description').val(description);


});
});





$('#form1').on('click', '.addButtonDED', function() {
var $template = $('.form-group:last').clone(true, true).find('input').val('').end()
.find('.addButtonDED').removeClass('addButtonDED').addClass('removeButtonDED').end()
.find('i').removeClass('fa-plus').addClass('fa-minus').end();
$template.insertAfter('.form-group:last');
})

// Remove button click handler
.on('click', '.removeButtonDED', function() {
var $row = $(this).closest('.form-group');
$row.remove();
});
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" method="post" class="form-horizontal" role="form">
<fieldset>


<div class="form-group">
<div class="col-md-2">
<input type="text" list="products" class="form-control product" name="product[]" />
<datalist id="products" class="products"></datalist>
</div>

<div class="col-md-4">
<input id="" type="text" class="form-control description" name=" description[]" />
</div>



<div class="col-md-1">
<button type="button" class="btn btn-default addButtonDED"><i class="fa fa-plus"></i></button>
</div>
</div>

<div class="col-md-10 ">
<button type="submit" name="formAction" value="next" class="btn btn-primary">sUBMIT</button>

</div>



</fieldset>
</form>

</fieldset>
</form>

要读取值,请执行循环:

foreach($_POST['product'] as $product) {
//do stuf
}

关于javascript - 在自动完成功能中断的表单中添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42339113/

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