简体   繁体   English

下拉框-onchange和onselect

[英]Dropdown box - onchange and onselect

I want help, i am very new to html.. 我需要帮助,我是html的新手。

On selecting option from dropdown menu, I want html to put the values in word.. eg When I select "1" from drop down, it must show "one" When I select "2' from drop down, it must show "two" 从下拉菜单中选择选项时,我希望html将值放在单词中。例如,当我从下拉列表中选择“ 1”时,它必须显示“一个”。当我从下拉列表中选择“ 2”时,它必须显示“两个” ”

How to do that?? 怎么做??

<HTML>
<Table border=10>

<TR>
<TD>Select Number</TD>
<TD><Select>
<option>1</option>
<option>2</option>
<option>3</option>
</Select></TD>
</TR>

<tr>
<td>In Words</td>
<td><input type="text" readonly></td>
</tr>

</Table>
</HTML>

Please make a script and show me... 请写剧本给我看...

If I understand what you want, you'll need some javascript to find what you selected, and take that 'string' and shove it in an element for the user to see. 如果我了解您想要的内容,则需要一些javascript来找到您选择的内容,并采用该“字符串”并将其推入元素中,以供用户查看。

Here is a working example. 这是一个工作示例。 Try making one these next time you ask a question. 下次您问一个问题时,尝试制作一个。 Welcome to Stack Overflow. 欢迎使用堆栈溢出。

http://jsfiddle.net/sheriffderek/6vp5Lskn/ http://jsfiddle.net/sheriffderek/6vp5Lskn/

HTML 的HTML

<select name='my_select' id='my_select'>

    <option value='1'>Option 1</option>
    <option value='2'>Option 2</option>
    <option value='3'>Option 3</option>
    <option value='4'>Option 4</option>
    <option value='5'>Option 5</option>

</select>

<div id="outcome">
    You have selected: <span></span>
</div>


javascript (jQuery) javascript(jQuery)

var selectedOption; // define this variable

// when the select is changed...
$('#my_select').on('change', function() {
    // get the option that was selected
    selectedOption = $( "#my_select option:selected" ).text();
    // put the option in the place you want it
    $('#outcome span').html(selectedOption);
});

A non-jQuery solution : 非jQuery解决方案

Firstly, give your select- and input-tags id's, and your options values (value=""). 首先,提供您的选择标签和输入标签ID,以及您的选项值(value =“”)。 Then add a onchange=""-listener in the select-tag and make a function that carries out what you want to do (ie checking the selected value and displaying it in your input field), like so: 然后在选择标签中添加一个onchange =“”-listener,并创建一个函数来执行您要执行的操作(即检查所选值并将其显示在输入字段中),如下所示:

 function showValue() { var x = document.getElementById("mySelect").value; document.getElementById("mySelection").value = "You selected: " + x; } 
 <Table border=10> <tr> <td>Select Number</td> <td><Select onchange="showValue()" id="mySelect"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </Select></td> </tr> <tr> <td>In Words</td> <td><input type="text" id="mySelection"></td> </tr> </Table> 

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

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