1

我使用两个文本框来求和数字但显示错误..

    <script>
        function getPrice(tMaking, tHandeling, tTotal) {
            var obj_tMaking = document.getElementById(txtMaking_F)
            var obj_tHandeling = document.getElementById(txtHandeling_F)
            var obj_tTotal = document.getElementById(txtTotal_F)
            if (obj_tMaking.value != "" && obj_tHandeling.value != "") {
                obj_tTotal.value = parseInt(obj_tMaking.value) + parseInt(obj_tHandeling.value);
            }
            else {
                obj_tTotal.value = "";
            }
        }
    </script>
</head>
<body>
    <input name="grdView$ctl08$txtMaking_F" type="text" id="grdView_ctl08_txtMaking_F" class="classMaking" onchange="javascript:return getPrice('grdView_ctl08_txtMaking_F','grdView_ctl08_txtHandeling_F','grdView_ctl08_txtTotal_F');" style="width: 60px;" />
    <input name="grdView$ctl08$txtHandeling_F" type="text" id="grdView_ctl08_txtHandeling_F" class="classHandling" onchange="javascript:return getPrice('grdView_ctl08_txtMaking_F','grdView_ctl08_txtHandeling_F','grdView_ctl08_txtTotal_F');" style="width: 60px;" />
    <input name="grdView$ctl08$txtTotal_F" type="text" id="grdView_ctl08_txtTotal_F" class="classTotal" style="width: 100px;" />
</body>

在此处输入图像描述

4

1 回答 1

2

我认为您的功能中有拼写错误

function getPrice(tMaking, tHandeling, tTotal) {
            var obj_tMaking = document.getElementById(tMaking)
            var obj_tHandeling = document.getElementById(tHandeling)
            var obj_tTotal = document.getElementById(tTotal)
                                   //                   ^  - removed extra "txt" & "_F"

            if (obj_tMaking.value != "" && obj_tHandeling.value != "") {
            obj_tTotal.value = parseInt(obj_tMaking.value) + parseInt(obj_tHandeling.value);
        }
        else {
            obj_tTotal.value = "";
        }
    }

我假设您希望传递给函数的值是 id,在这种情况下应该有tMakinginvar obj_tMaking = document.getElementById(txtMaking_F)和 not txtMaking_Fobj_tHandeling和相同obj_tTotal

于 2013-08-17T18:16:16.187 回答