如何使这两个javascript代码协同工作
How can I get these two javascript codes to work together?
我用html制作了一个表单。
一开始我觉得很简单。我的输入是用户输入的金额。然后,我编写了javascript代码,根据用户输入的金额计算动态价格。代码如下:
<input class="typeahead" type="text" placeholder="Amount" name="Gift-Card Amount"/>
javascript:
jQuery("input[name='Gift-Card Amount']").change(function () {
if (isNaN(parseFloat(this.value)) || !isFinite(this.value)) {
jQuery(this).val('');
return false;
}
var calc = parseFloat(this.value) * 0.95;
jQuery(this).parents("form").find("input[name='price']").val(calc);
});
计算结果为常数0.95。所以我添加了一个新的输入。商店名称。因此用户可以输入商店名称。金额:
<input class="stores typeahead" type="text" placeholder="Stores" name="name"/>
我希望价格能根据商店名称和数量而变化。所以我创建了这个对象:
var stores = {
"McDonalds" : .90,
"Target" : .92,
}
var storeName = jQuery(this).parents("form").find("input[name='name']").val();
console.log(stores[storeName]);
因此,该值可以替换为基于输入的商店名称的预设值,而不是常数0.95。我不知道如何让这两个人一起工作。也就是说,我如何对第一个javascript进行重新编码,以重新确定var存储值,而不是0.95?
jQuery("input[name='Gift-Card Amount']").change(function () {
var amount = parseFloat(this.value);
if (isNaN(amount) || !isFinite(amount)) {
jQuery(this).val('');
return false;
}
var storeName = jQuery(this).parents("form").find("input[name='name']").val();
if (storeName in stores) {
var calc = amount * stores[storeName];
jQuery(this).parents("form").find("input[name='price']").val(calc);
}
});
我还建议您将Stores
从文本输入更改为<select>
。这样,你就不依赖于用户正确拼写商店,包括大写字母。
<select name="name" class="storeName">
<option value="">Please select a store</option>
<option value=".90">McDonalds</option>
<option value=".92">Target</option>
</select>
然后你可以使用
var calc = parseFloat(jQuery(this).parents("form").find(".storeName").val());
我会这样做:
function calcPrice(element) {
// Use the element that called the listener to find the form
var form = element.form;
// Access form controls as named properties of the form
var amt = form["Gift-Card Amount"].value;
// Don't clear the value if it's not suitable, it's annoying
// Let the user fix it themselves
if (parseFloat(amt) != amt) {
alert("Gift card amount is not a suitable value")
}
// Set the price
form.price.value = amt * form.store.value;
}
</script>
示例表单,其中包含所有存储值。通过这种方式,您可以在服务器获取所有相关值的情况下进行回退,而不依赖于客户端计算。。
<form>
Store:
<select name="store" onchange="calcPrice(this)">
<option value="0.90">McDonalds
<option value="0.92">Target
</select>
<br>
Gift card amount:
<input name="Gift-Card Amount" onchange="calcPrice(this)">
Price:
<input name="price" readonly>
</form>
编写一个可以返回正确值的小函数
function retrieveStoreValue(store){
return stores[store];
} // be sure stores is defined before the first call of this one
在你的变更函数中称之为
var storeName = jQuery(this).parents("form").find("input[name='name']").val();
var calc = parseFloat(this.value) * (retrieveStoreValue(storeName) );
相关文章:
- 组合两个javascript函数
- 比较包含多个值对的两个JavaScript数组
- 为什么这两个JavaScript函数调用具有相同的“;这个“;价值
- 无法延迟iPhone/iPad上的两个JavaScript操作
- 比较IF条件下的两个javascript数值
- 我可以在两个javascript文件之间传递一个字符串吗?
- 为标记调用href中的两个javascript函数
- 如何执行两个javascript函数
- 两个javascript网络工作者连续打印输出-为什么不同时打印呢
- 如何手动绑定两个javascript文件
- 两个javascript脚本的非独立性
- 合并两个Javascript数组,一次合并三个元素
- 为同一标记中的两个不同事件调用两个javascript方法
- 将两个Javascript函数组合到一个窗口中.nload不起作用
- 结合两个JavaScript函数
- IE可以同时执行两个JavaScript函数吗?
- 两个 JavaScript 文件,哪个方法首先调用
- 在 html 中的两个 JavaScript 之间传递值
- 两个 JavaScript 对象具有相同的属性实现方式,但原型不同
- 如何将两个Javascript函数合二为一