admin管理员组文章数量:1333201
Let's say you have this dropdown menu:
<select>
<option value="10">Apple</option>
<option value="20">Orange</option>
</select>
<input type="name" />
<input type="price" />
If user selects "Orange" from the dropdown menu, the textboxes' values should be:
<input type="name" value="Orange" />
<input type="price" value="20" />
or if it was "Apple", then textboxes' values should be Apple
and 10
.
Is this possible with Jquery or Jquery Mobile, if yes how?
Let's say you have this dropdown menu:
<select>
<option value="10">Apple</option>
<option value="20">Orange</option>
</select>
<input type="name" />
<input type="price" />
If user selects "Orange" from the dropdown menu, the textboxes' values should be:
<input type="name" value="Orange" />
<input type="price" value="20" />
or if it was "Apple", then textboxes' values should be Apple
and 10
.
Is this possible with Jquery or Jquery Mobile, if yes how?
Share Improve this question asked Apr 19, 2013 at 19:25 IMBIMB 15.9k23 gold badges87 silver badges149 bronze badges 4- 3 Yes, yes it is possible. – j08691 Commented Apr 19, 2013 at 19:26
- 1 @IMB what have you tried? ;-) – Dom Commented Apr 19, 2013 at 19:28
- Well all your answers work, now it will tough to select best answer :-) Thanks! – IMB Commented Apr 19, 2013 at 19:44
- @IMB choose PSCoder's, that's the best xD – Hanlet Escaño Commented Apr 19, 2013 at 19:52
4 Answers
Reset to default 4http://jsfiddle/f23uP/
$('select').change(function(){
$('input[type=name]').val($('option:selected',this).text());
$('input[type=price]').val($(this).val());
});
Yes
$("select").change(function() {
var price = this.value;
var selText = $("select option:selected").text();
$("input[type='name']").val(selText);
$("input[type='price']").val(price);
});
Something like that should work
jQuery:
$("#s").on("change",function(){
$("#text").val($(this).children().filter("option:selected").text());
$("#price").val($(this).val());
});
Markup:
<select id="s">
<option value="10">Apple</option>
<option value="20">Orange</option>
</select>
<input type="text" id="text" />
<input type="text" id="price" />
jsFiddle: http://jsfiddle/hescano/NnsDt/
I think you are a little confused with types of inputs. This MDN link will help.
If I understand you correctly, you want to take the value and the label from the select and store them in a corresponding textbox. Here is my solution:
HTML:
<select id="fruit">
<option></option>
<option value="10">Apple</option>
<option value="20">Orange</option>
</select>
<input type="text" id="name" />
<input type="text" id="price" />
jQuery:
$("#fruit").change(function() {
var name = $(this).find(":selected").text();
var price = $(this).val();
$("#name").val(name);
$("#price").val(price);
});
Note that this jQuery only binds the fruit select
for the onchange instead of binding all select
本文标签: javascriptCopy dropdown menu39s data to text box on selectStack Overflow
版权声明:本文标题:javascript - Copy dropdown menu's data to text box on select? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742276311a2445264.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论