Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以根据用户表单输入更改动态javascript变量吗?_Javascript_Php_Jquery_Html - Fatal编程技术网

我可以根据用户表单输入更改动态javascript变量吗?

我可以根据用户表单输入更改动态javascript变量吗?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我这里的问题是,是否有一种方法可以根据用户在输入字段中输入的名称更改动态价格计算 我的代码可以很好地计算html表单中的动态价格。代码如下: 使用此输入: <input class="typeahead" type="text" placeholder="Amount" name="Amount"/> 这本身就非常有效。它按.95计算金额,然后将该值指定为价格 如果我将此添加到表格中: <input class="stores typeahead" type="text" p

我这里的问题是,是否有一种方法可以根据用户在输入字段中输入的名称更改动态价格计算

我的代码可以很好地计算html表单中的动态价格。代码如下:

使用此输入:

<input class="typeahead" type="text" placeholder="Amount" name="Amount"/>
这本身就非常有效。它按.95计算金额,然后将该值指定为价格

如果我将此添加到表格中:

<input class="stores typeahead" type="text" placeholder="Stores" name="name"/>

我可以在这里做些什么,以便根据商店名称计算不同值的价格。例如,如果有人进入麦当劳,我希望它计算为.90。如果有人进入目标,我希望它计算为.92。以前的javascript无法实现这一点,因为它以.95计算所有内容,而不能根据输入的存储进行更改


我更愿意用javascript来完成这一点,因为我对php不是很熟练。

您可以为此创建一个javascript对象

var stores = {
     "McDonalds" : .90,
     "Target" : .92,
}
var storeName = jQuery(this).parents("form").find("input[name='name']").val();
console.log(stores[storeName]); //Output the store cost to console.

尽管我认为jQuery查找函数有问题。我相信有更好的方法来选择这个文本框。但这正是您需要的一般概念。

谢谢!这正是我所需要的。我不知道为什么有人来这里打-1,我想我的问题很清楚。您的回答非常完美。您可以使用
商店[storeName]
而不是当前使用的号码(0.95)。我建议您学习更多关于Javascript的基础知识,因为您似乎对它相当陌生。
var stores = {
     "McDonalds" : .90,
     "Target" : .92,
}
var storeName = jQuery(this).parents("form").find("input[name='name']").val();
console.log(stores[storeName]); //Output the store cost to console.