gpt4 book ai didi

Javascriptcalculate() 函数在 Firefox 和 Safari 中不起作用

转载 作者:行者123 更新时间:2023-11-28 00:33:26 25 4
gpt4 key购买 nike

我已经设置了一个表单,可以使用 JavaScript 中的Calculate() 函数执行某些基本计算。它有 5 个数量选择框,当您更改数量时,“升”和“总量”会更新。

该表单在 Chrome 和 Opera 中完美运行,但什么也没做!在 Firefox 和 Safari 中。

您可以在这里查看 –  http://jsfiddle.net/bksuru/ajsggpcg/

<小时/>

HTML:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="60" colspan="5" valign="top"><h2>Variants:</h2></td>
</tr>
<tr>
<td height="40" align="left"><div class="variant">Flowers:</div></td>
<td height="45" align="right" style="padding-right:14px;"><label>Size<select name="flowers_size" id="flsz" type="text" size="1" class="select2" tabindex="1" ><option selected="selected">200 ML</option></select></label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Price<input name="flowers_price" type="text" class="input5" id="flpr" tabindex="1" value="MRP 1495.00" readonly="readonly" /></label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Quantity<select name="flowers_quantity" id="flqt" type="text" size="1" class="select3" tabindex="1" oninput="calculate()">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select></label></td>
<td height="45" align="right"><label>Litres<input name="flowers_litres" type="text" class="input6" id="fllt" tabindex="1" value="0.000" readonly="readonly" /></label></td>
</tr>
<tr>
<td height="40" align="left"><div class="variant">Fruits:</div></td>
<td height="45" align="right" style="padding-right:14px;"><label>Size<select name="fruits_size" id="frsz" type="text" size="1" class="select2" tabindex="1" ><option selected="selected">200 ML</option></select></label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Price<input name="fuits_price" id="frpr" type="text" class="input5" tabindex="1" value="MRP 1295.00" readonly="readonly" />
</label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Quantity<select name="fruits_quantity" id="frqt" type="text" class="select3" size="1" tabindex="1" oninput="calculate()">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label></td>
<td height="45" align="right"><label>Litres<input name="fruit_litres" id="frlt" type="text" class="input6" tabindex="1" value="0.000" readonly="readonly" />
</label></td>
</tr>
<tr>
<td height="40" align="left"><div class="variant">Plantations:</div></td>
<td height="45" align="right" style="padding-right:14px;"><label>Size<select name="flowers_size" id="flsz" type="text" size="1" class="select2" tabindex="1" ><option selected="selected">200 ML</option></select></label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Price<input name="flowers_price" id="plpr" type="text" class="input5" tabindex="1" value="MRP 1195.00" readonly="readonly" />
</label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Quantity<select name="flowers_quantity" id="plqt" size="1" class="select3" tabindex="1" oninput="calculate()">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label></td>
<td height="45" align="right"><label>Litres<input name="plantations_litres" id="pllt" type="text" class="input6" tabindex="1" value="0.000" readonly="readonly" />
</label></td>
</tr>
<tr>
<td height="40" align="left"><div class="variant">Vegetables:</div></td>
<td height="45" align="right" style="padding-right:14px;"><label>Size<select name="vegetables_size" id="vesz" type="text" size="1" class="select2" tabindex="1" ><option selected="selected">200 ML</option></select></label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Price<input name="vegetables_price" id="vepr" type="text" class="input5" tabindex="1" value="MRP 995.00" readonly="readonly" />
</label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Quantity<select name="vegetables_quantity" id="veqt" size="1" class="select3" tabindex="1" oninput="calculate()">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label></td>
<td height="45" align="right"><label>Litres<input name="vegetables_quantity" type="text" class="input6" id="velt" tabindex="1" value="0.000" readonly="readonly" />
</label></td>
</tr>
<tr>
<td height="40" align="left"><div class="variant">Cereals:</div></td>
<td height="45" align="right" style="padding-right:14px;"><label>Size<select name="cereals_size" id="cesz" type="text" size="1" class="select2" tabindex="1" ><option selected="selected">200 ML</option></select></label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Price<input name="cereals_price" id="cepr" type="text" class="input5" tabindex="1" value="MRP 895.00" readonly="readonly" />
</label></td>
<td height="45" align="right" style="padding-right:14px;"><label>Quantity<select name="cereals_quantity" id="ceqt" size="1" class="select3" tabindex="1" oninput="calculate()">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label></td>
<td height="45" align="right"><label>Litres<input name="cereals_litres" id="celt" type="text" class="input6" tabindex="1" value="0.000" readonly="readonly" />
</label></td>
</tr>
<tr>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="total">
<tr>
<td align="left" valign="middle"><label class="variant">Total Amount:<input name="total_amount" type="text" class="input7" id="tamt" tabindex="1" value="MRP 0.00" readonly="readonly" /></label></td>
<td align="right" valign="middle"><input name="Submit" type="button" class="button" value="SUBMIT"/></td>
</tr>
</table>
</tr>
</table>
<小时/>

JavaScript:

function calculate() {
var flowersSize = 200;
var flowersPrice = 1495;

var fruitsSize = 200;
var fruitsPrice = 1295;

var plantationsSize = 200;
var plantationsPrice = 1195;

var vegetablesSize = 200;
var vegetablesPrice = 995;

var cerealsSize = 200;
var cerealsPrice = 895;

var flowersQuantity = flqt.options[flqt.selectedIndex].value;
var fruitsQuantity = frqt.options[frqt.selectedIndex].value;
var plantationsQuantity = plqt.options[plqt.selectedIndex].value;
var vegetablesQuantity = veqt.options[veqt.selectedIndex].value;
var cerealsQuantity = ceqt.options[ceqt.selectedIndex].value;

var flowersLitres = flowersSize * flowersQuantity / 1000;
var fruitsLitres = fruitsSize * fruitsQuantity / 1000;
var plantationsLitres = plantationsSize * plantationsQuantity / 1000;
var vegetablesLitres = vegetablesSize * vegetablesQuantity / 1000;
var cerealsLitres = vegetablesSize * cerealsQuantity / 1000;

flowersLitres = parseFloat(Math.round(flowersLitres * 100) / 100).toFixed(3);
fruitsLitres = parseFloat(Math.round(fruitsLitres * 100) / 100).toFixed(3);
plantationsLitres = parseFloat(Math.round(plantationsLitres * 100) / 100).toFixed(3);
vegetablesLitres = parseFloat(Math.round(vegetablesLitres * 100) / 100).toFixed(3);
cerealsLitres = parseFloat(Math.round(cerealsLitres * 100) / 100).toFixed(3);

fllt.value = flowersLitres;
frlt.value = fruitsLitres;
pllt.value = plantationsLitres;
velt.value = vegetablesLitres;
celt.value = cerealsLitres;

var totalAmount = flowersQuantity * flowersPrice + fruitsQuantity * fruitsPrice + plantationsQuantity * plantationsPrice + vegetablesQuantity * vegetablesPrice + cerealsQuantity * cerealsPrice;

totalAmount = parseFloat(Math.round(totalAmount * 100) / 100).toFixed(2);

tamt.value = "MRP"+" "+totalAmount.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
<小时/>

非常感谢任何帮助。

谢谢!

BK苏鲁

最佳答案

您正在使用onInput 事件。此事件适用于 inputtextarea 元素。 选择 元素。
更具体地说

<input type="password">
<input type="search">
<input type="text">
<textarea>

使用 onChange 事件代替。

显然,某些浏览器确实支持其他元素上的 onInput,但这不是标准。

编辑(添加示例):

<select name="flowers_quantity" ... oninput="calculate()">

将其更改为:

<select name="flowers_quantity" ... onchange="calculate()">

关于Javascriptcalculate() 函数在 Firefox 和 Safari 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28737615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com