简体   繁体   English

Jquery 自动完成 - 在多个输入字段中添加值

[英]Jquery autocomplete - add value in multiple input fields

I need help with jquery autocomplete.我需要 jquery 自动完成方面的帮助。 The autocomplete by itself works fine, but I need to fill a value in another input field depending on the value of the first input.自动完成本身工作正常,但我需要根据第一个输入的值在另一个输入字段中填充一个值。

I created https://jsfiddle.net/peh9c20a/ to show this problem.我创建了https://jsfiddle.net/peh9c20a/来显示这个问题。

I have a form with three rows, each row has two input fields.我有一个包含三行的表单,每行有两个输入字段。 Autocomplete is implemented on the first column of inputs.自动完成是在第一列输入上实现的。

When selecting a value from the autocomplete (first column), the desired behavior is to fill the input field next to it with a random number.从自动完成(第一列)中选择一个值时,所需的行为是用一个随机数填充它旁边的输入字段。 I am unfortunately able to fill all the inputs with a number, not only the input in the same row.不幸的是,我能够用数字填充所有输入,而不仅仅是同一行中的输入。

To describe this problem with the text is a little bit complicated, but I hope that the fiddle will help to understand it.用文字来描述这个问题有点复杂,但我希望小提琴能帮助理解它。

The form is made dynamically with clone() function, I can´t use different ID attributes for each input.该表单是使用 clone() function 动态创建的,我不能为每个输入使用不同的 ID 属性。

 $('.item-name').autocomplete({
    lookup:sourceData,
    onSelect: function (suggestion) {
       var number = Math.floor(Math.random()*100);
        $(this).closest($(".item-id").val(number)); //THIS ROW HAS TO BE MODIFIED   
        }
    });

do that:去做:

to access of the input number linked to the input text, you have to go up to the div which is the parent of both.要访问链接到输入文本的输入数字,您必须 go 直到作为两者父级的 div。 (sorry for my english) (对不起我的英语不好)

$(this) => input changed $(this) => 输入改变

.closest("div.row") => find the first parent with div.row .closest("div.row") => 用div.row找到第一个父级

.find("input[type=number]") => find the input number linked to the text .find("input[type=number]") => 查找链接到文本的输入数字

   $('.item-name').autocomplete({
    lookup:sourceData,
    onSelect: function (suggestion) {
       var number = Math.floor(Math.random()*100);

        $(this).closest("div.row").find("input[type=number]").val(number); 
        }
    });

 var sourceData = [ "Peter", "John", "Adam", "Zack", "George", "Richard", "Brian", "Frank", "Lars", "Quentin", "Will" ]; $('.item-name').autocomplete({ lookup: sourceData, onSelect: function(suggestion) { var number = Math.floor(Math.random() * 100); $(this).closest("div.row").find("input[type=number]").val(number); } });
 .row { background: #f8f9fa; margin-top: 20px; }.col { border: solid 1px #6c757d; padding: 10px; }
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" /> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.devbridge-autocomplete/1.2.27/jquery.autocomplete.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script> <div id="meal-container"> <div class="col-12" id="meal-div"> <div class="form-group"> <div class="row"> <div class="col-7"> <input type="text" placeholder="Add name" class="form-control item-name" name="item-name[]" /> </div> <div class="col-5"> <input type="number" class="form-control item-id" name="id[]" /> </div> </div> </div> </div> <div class="col-12" id="meal-div"> <div class="form-group"> <div class="row"> <div class="col-7"> <input type="text" placeholder="Add name" class="form-control item-name" name="item-name[]" /> </div> <div class="col-5"> <input type="number" class="form-control item-id" name="id[]" /> </div> </div> </div> </div> <div class="col-12" id="meal-div"> <div class="form-group"> <div class="row"> <div class="col-7"> <input type="text" placeholder="Add name" class="form-control item-name" name="item-name[]" /> </div> <div class="col-5"> <input type="number" class="form-control item-id" name="id[]" /> </div> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM