jQuery代码.val();不在FF工作

时间:2010-12-30 22:25:35

标签: javascript jquery

我有这段代码

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" />

此代码在IE中工作非常好,但它在FF中不起作用。

什么是问题?

提前致谢。

编辑:

<!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>

4 个答案:

答案 0 :(得分:4)

使用

$(this).val()

而不是

this.value

答案 1 :(得分:2)

问题是,对于包含您的功能的脚本,您有type="text/jscript"而不是type="text/javascript"

答案 2 :(得分:1)

试试这个:

<强> 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();
    });
});

答案 3 :(得分:0)

您还可以使用以下内容:

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