简体   繁体   English

根据下拉选择填充文本框

[英]Populate text box based on drop down selection

I have this javascript function hoping to populate a textbox based on drop down selection. 我有此javascript函数,希望根据下拉选择来填充文本框。 I am just using the first option (there are 7 more options, but this should illustrate the issue): 我只是使用第一个选项(还有7个选项,但这应该可以说明问题):

<script>
function TicketsQuantity(){
  if (document.getElementByID("RaffleDollars").value == 25){
    (document.getElementById("RaffleTickets").value = "1");
  }
}
</script>    

Here is the drop down box code: 这是下拉框代码:

<select size="1" name="RaffleDollars" id="RaffleDollars" 
onChange="javascript:TicketsQuantity();">               
<option selected value="0.00">------Select $ Amount------</option>
<option value="25">25.00</option>
</select>

Here is the text box code: 这是文本框代码:

<input name="RaffleTickets" size="5" id="RaffleTickets">

When I select 25.00 from the drop down box, it is not populating textbox with 1. Is my syntax incorrect? 当我从下拉框中选择25.00时,它没有用1填充文本框。我的语法不正确吗? Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

You have an error: getElementByID is incorrect. 您有一个错误: getElementByID不正确。 Change it to: getElementById . 将其更改为: getElementById

Please correct your code to use the below. 请更正您的代码以使用以下内容。 Remove javascript: here: 删除javascript:此处:

onChange="TicketsQuantity();"

Since you are using , I would give you a jQuery based solution: 由于您正在使用 ,因此我将为您提供一个基于jQuery的解决方案:

$(function () {
  $("#RaffleDollars").change(function () {
    if ($(this).val() == "25")
      $("#RaffleTickets").val("1");
  });
});

Working Snippet 工作片段

 <select size="1" name="RaffleDollars" id="RaffleDollars" onChange="TicketsQuantity();"> <option selected value="0.00">------Select $ Amount------</option> <option value="25">25.00</option> </select> <input name="RaffleTickets" size="5" id="RaffleTickets"> <script> function TicketsQuantity() { if (document.getElementById("RaffleDollars").value == 25) { document.getElementById("RaffleTickets").value = "1"; } } </script> 

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

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