问题描述
我正在尝试使用所选选项的值填充输入字段,两者都是动态加载的.我能够加载选定的选项值,并且可以在选项更改时对其进行更新.但是如果有10个输入字段和10个选择选项标签,并且我从10个标签中选择一个选项,则所有10个输入字段都显示相同的值.每个输入字段都有10个选项.我需要单独更改值.
I am trying to populate input field with the value of selected option both are dynamically loaded. I am able to load selected option value and can update it when option changed. but if there are 10 input field and 10 select option tags and i select one option from 10 tags than all 10 input fields show same value. Every input field have 10 options. I need to change value individually .
这是jquery的功能
here is function of jquery,
$("#myselect").change(function () {
$( "select option:selected" ).each(function() {
$('.returnValue').val($( this ).text());
});
});
我要在输入字段中插入单个产品数量.
I want to insert individual product quantity in input field.
我苦苦挣扎了两天,请帮忙.
I am struggling for two days please help.
<select id="myselect">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input class="returnValue" type="text" value="">
推荐答案
您可以通过多种方式解决您的问题:
You can solve your problem in different ways:
如果选择字段和输入字段是一个接一个的,则可以使用:
If the select and input fields are one after another you may use:
$(this).next('.returnValue').val($( this ).val());
另一种方法可以基于为每个选择添加一个新属性,例如:
Another approach can be based on adding for each select a new attribute like:
input-field="newlass1"
因此,以一种链接选择和输入字段的方式将新的类添加到相应的输入字段.
And so add a new class to the corresponding input field in a way to link the select and input field.
在这种情况下,您可以这样做:
In this case you can do it:
$('input.' + $(this).attr('input-field')).val($( this ).val());
摘要:
$("[id^=myselect]").change(function () {
//$(this).next('.returnValue').val($( this ).val());
$('input.' + $(this).attr('input-field')).val($( this ).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select id="myselect1" input-field="newlass1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input class="returnValue newlass1" type="text" value=""><br>
<select id="myselect2" input-field="newlass2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input class="returnValue newlass2" type="text" value=""><br>
<select id="myselect3" input-field="newlass3">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input class="returnValue newlass3" type="text" value=""><br>
<select id="myselect4" input-field="newlass4">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input class="returnValue newlass4" type="text" value="">
这篇关于如何在jQuery中使用动态选择选项填充动态输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!