繁体   English   中英

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

[英]Javascript calculate() function not working in Firefox and Safari

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

该表单可以在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 Suru

您正在使用onInput事件。 此事件适用于inputtextarea元素。 select元素。
进一步来说

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

改用onChange事件。

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

编辑(添加示例):

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

更改为:

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

只需将方法oninput更改为onchange

暂无
暂无

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

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