简体   繁体   English

从动态生成的输入字段获取内部数据而不保存值-jQuery

[英]Getting data inside drop down from dynamically generated input fields without values being saved - jquery

i have dynamically generated input fields through user input. 我已经通过用户输入动态生成了输入字段。 My form is not being saved. 我的表格未保存。 i have to get the values from all input fields and populate them in a drop down select box. 我必须从所有输入字段中获取值,然后在下拉选择框中填充它们。

Also, are there any other ways to do it ? 另外,还有其他方法吗? is this the right method to do this ? 这是正确的方法吗?

Doing it this way is about the least efficient way to approach the problem. 这样做是解决问题的最不有效方法。

$("input.className").each(function (index,element)
{
    var inputVal = $("element").val();
    $("select").append('<option>'+inputVal+'</option');
});

First you're hitting the dom to find the input. 首先,您要在dom上找到输入。

Then you're using jquery each to loop through the input which returns every single time it loops. 然后,您使用jquery each遍历输入,该输入每循环一次返回一次。

On "each" pass through the loop you're hitting the dom again. 在“每个”循环上,您会再次碰到dom。

Please see the first answer i posted. 请参阅我发布的第一个答案。 While your approach "works" if you ever have to write something on the large side this approach will no longer be valid. 尽管您的方法“行之有效”,但如果您不得不在较大的方面写点东西,则此方法将不再有效。

When you ask "how" be sure to ask "what is the most performant way to do this?". 当您问“如何”时,一定要问“最有效的方法是什么?”。 Otherwise you're going to accumulate a lot of bad habits which will make you incapable of writing anything large. 否则,您将积累许多不良习惯,这将使您无力编写任何大型文章。

You can add the values of input elements with the same class to a select element like so: 您可以将具有相同类的输入元素的值添加到选择元素,如下所示:

$("input.className").each(function (index,element)
{
    var inputVal = $("element").val();
    $("select").append('<option>'+inputVal+'</option');
});

If your page is not being generated dynamically you should first grab all of your inputs. 如果页面不是动态生成的,则应首先获取所有输入。 If it is being generated dynamically your dom nodes should already be stored in an object. 如果它是动态生成的,则您的dom节点应该已经存储在一个对象中。

Say that you have multiple input sections on a page (not generated dynamically), each denoted with an id: 假设您在页面上有多个输入部分(不是动态生成的),每个部分都用一个id表示:

<div class="formSection" id="dropDownEntry">
    <label for="dropDown1">Drop1</label>
    <input id="dropDown1"></input>
    <label for="dropDown2">Drop1</label>
    <input id="dropDown2"></input>
</div>

Now when you go to grab the fields you should only do one dom lookup if possible. 现在,当您去抢夺田地时,如果可能的话,您应该只进行一次dom查找。 So you grab the parent node, and then instruct jquery, or js to get the children with a tag of "input". 因此,您获取父节点,然后指示jquery或js用“ input”标签获取子节点。

var dropDownInputsArray = $('#dropDownEntry').find('input');

Ok great now you have an array of inputs, and you've stored it inside of your function outside of the global scope. 好了,现在您有了一个输入数组,并且已经将其存储在全局范围之外的函数内部。 DO NOT USE $.each. 请勿使用$ .each。 It's a bad habit to get into because it's slow as hell. 这是一个坏习惯,因为它慢得要命。 I literally never use it. 我从不使用它。 Ever, and it's not hard to avoid. 永远,而且不难避免。 Iterate through your array of dom nodes and be sure to cache the length on init like so: 遍历dom节点数组,并确保在init上缓存长度,如下所示:

for(i=0, len=dropDownInputsArray.length; i < len; i++){

}

But WAIT, you first need to create a non-globally scoped node to store you're select nodes in, so that you're not appending stuff to the dom iteratively. 但是,等待,您首先需要创建一个非全局范围的节点来存储您要选择的节点,这样您就不会迭代地将内容添加到dom上。 The name of the game is DON"T TOUCH THE GD DOM. 游戏的名称是“不要触摸GD DOM”。

var mySelectNode = $('<select/>');
//len caches the length so you don't have to look it up each time.  Performance ++
 for(i=0, len=dropDownInputsArray.length; i < len; i++){
      var inputNodeVal = dropDownInputsArray[i].value;
      if(inputNodeVal.length > 0) //was anything entered there?
        mySelectNode.append($('<option/>', {
                   value: inputNodeVal,
                   text: inputNodeval
               }));
 }

//now append your mySelectNode to the dom or replace the previous select node with it.  We did it this way because before you actually attach a node to the dom you have stayed in javascript land which is 'cheaper' than touching dom-land.  

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

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