简体   繁体   English

我怎样才能把选择的选项 <select>从动态生成的表单到输入字段

[英]How can I put the selected option of a <select> from a dynamically generated form into an input field

I have a form, where you can dynamically add steps by clicking a button. 我有一个表单,您可以在其中通过单击按钮来动态添加步骤。 Each step contains different input fields. 每个步骤包含不同的输入字段。 One step has a ,which is filled with some options like this. 第一步有一个,其中包含一些类似的选项。

 <select id="zutatenListe" name="zutatenListe" onchange="updateZutaten()">
    <option value="0">Tomate</option>
    <option value="1">Brot</option>
</select>

It is possible that there are multiple of it, all with the same id and name. 它可能有多个,并且都具有相同的ID和名称。 Next to the select, there is always an input field like this: 在选择旁边,总是有一个像这样的输入字段:

<input id="selectedZutat" type="text" value="" name="wiegen_zutat[]">

What I want to make is that when you change the selected option, your selected option will be shown only in the input element next to the changed select. 我要说的是,当您更改所选选项时,所选选项将仅显示在更改的选项旁边的输入元素中。 It works for the first one, but for all other selects, it doesn't. 它适用于第一个选择,但不适用于所有其他选择。 My code is this: 我的代码是这样的:

function updateZutaten(){
        var eingabe;
        eingabe = $("#zutatenListe option:selected").text();    
        $( "#selectedZutat").val(eingabe);
}

My guess is that it only works for the first select element, because the other select elements have the same id. 我的猜测是,它仅适用于第一个选择元素,因为其他选择元素具有相同的ID。 Has anyone an idea how to take care of this problem? 有谁知道如何解决这个问题? Please don't get confused by the German names, but I'm from Germany. 请不要对德国的名字感到困惑,但是我来自德国。

Thank you everyone :) 谢谢大家 :)

Identifiers must be unique and as you are using bind event using it. 标识符必须是唯一的,并且在使用绑定事件时要使用它。

Add a common class to target the <select> element, this will help you select them using Class Selector (".class") and bind event using it, then you can use .next() target the next <input> element. 添加一个公共类以<select>元素为目标,这将帮助您使用Class Selector(“ .class”)选择它们并使用它绑定事件,然后您可以使用.next()定位下一个<input>元素。

Here in exmple I have added zutatenListe class to <select> element. 例如,我在这里将zutatenListe类添加到<select>元素中。

 $(function() { $(document).on('change', '.zutatenListe', function() { var text = $(this).find("option:selected").text(); $(this).next(".selectedZutat").val(text); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <select class="zutatenListe" name="zutatenListe"> <option value="0">Tomate</option> <option value="1">Brot</option> </select> <input class="selectedZutat" type="text" value="" name="wiegen_zutat[]"> </div> <div> <select class="zutatenListe" name="zutatenListe"> <option value="0">Tomate</option> <option value="1">Brot</option> </select> <input class="selectedZutat" type="text" value="" name="wiegen_zutat[]"> </div> 

Maybe work on the change() event? 也许在change()事件上工作?

$("#zutatenListe").change( function () {
        var eingabe;
        eingabe = $("#zutatenListe option:selected").text();    
        $( "#selectedZutat").val(eingabe);
})

So it will always set the selected option when the change event fires? 因此,它将在更改事件触发时始终设置选定的选项吗?

Use unique ID attributes for each set of input and selects. 对每组输入和选择使用唯一的ID属性。 HTML and Jquery require this. HTML和Jquery要求此。

HTML4 Spec HTML5 Spec HTML4规范 HTML5规范

Here's a way to do it in JavaScript. 这是使用JavaScript的一种方法。

 function updateZutaten(element) { var option = element.options[element.selectedIndex]; var parent = element.parentElement; var textBox = parent.getElementsByTagName("input")[0]; textBox.value = option.text; } 
 .formItems { list-style-type:none; padding:0; margin:0; } .formItems li { border-bottom:1px solid #EEE; padding: 10px 0; } 
 <form id="myForm"> <ul class='formItems'> <li> <select id="zutatenListe" name="zutatenListe" onchange="updateZutaten(this)"> <option value="">--Choose--</option> <option value="0">Tomate</option> <option value="1">Brot</option> </select> <input id="selectedZutat" type="text" value="" name="wiegen_zutat[]" /> </li> <li> <select id="zutatenListe" name="zutatenListe" onchange="updateZutaten(this)"> <option value="">--Choose--</option> <option value="0">Tomate</option> <option value="1">Brot</option> </select> <input id="selectedZutat" type="text" value="" name="wiegen_zutat[]" /> </li> </ul> </form> 

暂无
暂无

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

相关问题 如何将选择的选项与用户插入的输入放在一起? - How can I put together the option selected with the input inserted by the user? 我怎样才能得到一个<select>字段根据来自另一个的输入动态更改<select>React 中的字段? - How can I get a <select> field to change dynamically based on the input from another <select> field in React? 如何根据从下拉菜单中选择的选项设置不同的输入字段模式? - How can I set different pattern of input field based on the option selected from the dropdown menu? 如何从通过 Javascript function 动态创建的输入字段访问表单输入? - How can I access form input from an input field which was created dynamically via a Javascript function? HTML选择表单-如何触发“动态生成的选项” - HTML select form - how to trigger “dynamically generated option” 如何使用 jquery 将输入字段中的值传递给 select 选项? - How can I pass a value from input field to a select option using jquery? 如何添加输入字段并动态选择? - How can I add input field and select dynamically? 如何基于选择的选择选项动态更改输入值属性? - How to change dynamically input value attribute based on select option selected? 如何将 select 输入动态匹配到 Materialise CSS 中的选定选项? - How to match select input to selected option dynamically in Materialize CSS? 如何从 select 中的选定选项访问 object Id? - How can I access object Id from selected option in select?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM