开发者

jQuery code .val(); not working in FF

开发者 https://www.devze.com 2023-02-02 09:03 出处:网络
I have this code function calculateTotal() { var total = 0; $(\".quantity\").each(function() { if (!isNaN(this.value) && this.value.length != 0)

I have this code

function calculateTotal() {
    var total = 0;      
    $(".quantity").each(function() 
                                 {
                                     if (!isNaN(this.value) && this.value.length != 0) 
                                     {             
                                     total += parseFloat(this.value);         
                                     }     
                                     });      
    $("#total_quantity").val(total); 
}

 <input onchange="calculateTotal();"  name="sol1" type="text" class="result_form_textbox_small quantity" id="sol1" />

 <input name="total_quantity" type="text" class="result_form_textbox_small" id="total_quantity" />

This code is working in IE very good but it's not working in FF.

What is the proplem?

Thanks in advance.

EDIT :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=开发者_运维知识库"Content-Type" content="text/html; charset=utf-8" />
<<script type="text/javascript" src="javascripts/jquery-1.4.2.js"></script>
<script type="text/javascript" src="javascripts/jquery.mousewheel.js"></script>
<script type="text/javascript" src="javascripts/jquery.jscrollpane.min.js"></script>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="stylesheets/public.css" rel="stylesheet" type="text/css" />
 <script type="text/jscript">
function calculateTotal() {
    var total = 0;      
    $(".quantity").each(function() 
                                 {
                                     if (!isNaN(this.value) && this.value.length != 0) 
                                     {             
                                     total += parseFloat(this.value);         
                                     }     
                                     });      
    $("#total_quantity").val(total); 
} 
</script>
</head>


use

$(this).val()

instead of

this.value


The issue is that you have type="text/jscript" instead of type="text/javascript" for the script that contains your function.


Try this:

HTML

 <input  name="sol1" type="text" class="result_form_textbox_small quantity" id="sol1" />

 <input name="total_quantity" type="text" class="result_form_textbox_small" id="total_quantity" />

JS

 function calculateSum() {

        var sum = 0;
        //iterate through each textboxes and add the values
        $(".quantity").each(function() {
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#total_quantity").val(sum.toFixed(2));
    }


$(document).ready(function() {

    $('.quantity').keyup(function() { 
            calculateSum();
    });
});


You can also use the following:

$(this).attr("value","");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号