根据选择选项更新输入值 [英] Update input value depending on select option

查看:72
本文介绍了根据选择选项更新输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出更新某些输入值的最佳方法,具体取决于选择

I'm trying to figure out what's the best way to update some input value depending on the selected option from a select.

这是我想要实现的目标:

Here is what I want to achieve :

我有一个显示详细信息的页面域名。我有一个表格,其中输入选择,这样可以更改价格。 输入包含当前域名价格并允许用户输入新价格,但用户不必这样做,而是可以使用选择,其中包含3个不同货币的3个选项,一旦选择了一个,它应该根据默认价格更改新价格输入的值来自数据库的给定货币。

I have a page that displays details of a domain name. I have a form with input and select which allows the price to be changed. The input contains the current domain name price and allows user to put a new price in, however the user don't have to do it and instead can use the select which have 3 options with 3 different currencies and once one is selected it should change the value of the new price input based on default price of given currency from the database.

然后它变得有点复杂,因为脚本需要知道要在数据库中查找的TLD才能找到正确的价格。我想我需要将它作为参数传递给某个地方......

Then it gets a little more complicated, because the script needs to know the TLD to lookup at the Database in order to find the correct price. I guess I need to pass it as a parameter somewhere...

代码本身在提交时工作得很好,并且当选择了不同的货币时它会应用新的价格,但我只能在提交后看到它,我想避免的事情。

The code itself works perfectly fine when submitted, and it applies the new price when different currency has been selected, but I can only see it after submitting, something I want to avoid.

我不知道该怎么做。我应该使用jQuery本身还是jQuery AJAX或JS?我对所有这些异步的东西都很陌生,不知道从哪里开始,真的。

I'm not sure how to do it. Should I use jQuery itself or jQuery AJAX or JS ? I'm pretty new to all of this async stuff and don't know where to start, really.

推荐答案

能够要做到这一点javascript很好。你需要了解事件。当指定的操作发生时,事件会被触发 。在您的情况下,您希望在更改选择的值时触发它。因此你需要使用 onchange事件

To be able to do that javascript is fine. You need to learn about events. Events are triggered when a specified action takes place. In your case you want it to trigger when you change the value of a select. hence you will need to use the onchange event.

我创建了一个非常基本的例子sinc eyou没有提供任何代码。哪个你应该能够破译你想要的东西。

I created a really basic example sinc eyou didn't provide any code. Which you should be able to decipher to do what you want.

JAVASCRIPT

<script type="text/javascript">
function addEventsToHTML(){
    var cars = document.getElementById('cars');
    cars.onchange = changeHandler;
    function changeHandler(){
        alert("You changed to "+this.value);
    }
}
window.onload = addEventsToHTML;
</script>

HTML

  <select id="cars">
      <option value="Volvo">Volvo</option>
      <option value="MG">MG</option>
      <option value="Fiat">Fiat</option>
    </select>

查看现场演示!

这篇关于根据选择选项更新输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆