简体   繁体   English

根据下拉菜单中的选项为文本字段提供值

[英]Give textfield a value based on option selected from drop down

What I'm trying to do is give my textfield a value based an an option is select form my drop down. 我想做的是给我的文本字段一个值,一个选项是从下拉菜单中选择。 For example: I have 2 fields, a drop down and a textfield. 例如:我有2个字段,一个下拉列表和一个文本字段。 I select Facebook from the dropdown and the value " http://www.facebook.com/ " appears in my textfield. 我从下拉列表中选择Facebook,值“ http://www.facebook.com/ ”出现在我的文本字段中。 How can I achieve this effect? 我怎样才能达到这种效果? I know that I have to call a function onchange of the drop down but that's pretty much everything I know. 我知道我必须调用下拉菜单的onchange函数,但这几乎是我所知道的一切。 Remember that I'm not trying to copy the exact selected value from the dropdown to the textfield here. 请记住,我并不是要将下拉列表中的确切选定值复制到此处的文本字段。

example markup 示例标记

<select>
    <option value="http://www.facebook.com">Facebook</option>
    <option value="http://www.twitter.com">Twitter</option>
</select>

<input type="text" />

jquery jQuery的

$('select').change(function() {
    $('input[type="text"]').val(this.value);
});

Here's a fiddle 这是一个小提琴


In response to your comment, there are a number of ways to do it (a switch statement, if/elseif statement etc), the easiest would probably be to create an object mapping the text to the corresponding url : 为了回应您的评论,可以采用多种方法( switch语句, if/elseif语句等),最简单的方法可能是创建一个将text映射到相应url的对象:

var urlFromText = {
  'Facebook' : 'http://www.facebook.com/',
  'Twitter' : 'http://www.twitter.com/'
};

Then, in your change handler, you can simply use: 然后,在change处理程序中,您可以简单地使用:

$('input[type="text"]').val(urlFromText[$('option:selected', this).text()]);

Here's an example 这是一个例子

HTML 的HTML

<select id="network">
    <option value="http://www.facebook.com">Facebook</div>
    <option value="http://www.twitter.com">Twitter</div>
</select>
<input id="network-txt"/>

Jquery jQuery的

$("#network").change(function(){
    $("#network-txt").val($(this).val());
});

Working Example http://jsfiddle.net/nVEEE/ 工作示例 http://jsfiddle.net/nVEEE/

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

相关问题 从下拉列表中获取所选选项的值 - get the value of the selected option from the drop down 根据从其他组合框中选择的选项更改下拉选项 - Change the drop down option based on the selected option from other combobox 如何根据下拉菜单中的所选选项设置隐藏字段的值? - How to set value for hidden field based on selected option in drop down? 根据从下拉菜单中选择的选项创建查询字符串 - Creating a querystring based on what option is selected from a drop down menu 根据从下拉列表中选择的选项选择适当的复选框 - Selecting appropriate checkbox based upon Option selected from drop down 使用jQuery从下拉列表中获取所选选项的值。 - Get value of selected option from drop down using jquery. 从下拉菜单中选择的选项将填充另一个文本框/下拉值 - selected option from drop down fills another text box/drop down value value 从下拉列表中选择的选项将填充另一个文本框/下拉列表中的更改值 - selected option from drop down fills another text box/drop down value value on change OnChange 用于存储所选下拉选项的值 - OnChange for Storing Value of Selected Drop Down Option 如何根据从 AngularJs 中的下拉列表中选择的值来控制视图? - How to control the view based on the value selected from drop down in AngularJs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM