日期:2014-05-16  浏览次数:20395 次

JS的onlaod事件问题
的textbox里有一个事件getYinshua(this);他一开始是在onchange里
现在我想把他放在body的onlaod里,让这个页面一加载的时候就执行这个方法,但是出错了
原因是我的getYinshua(this);方法里有参数是动态的


我想让他加载的时候就能用该怎么办呢,求大神指点
方法如下:(错误就是id缺少对象)
function getYinshua(obj) {
var pre=obj.id.split("_")[1];
var ysling = Number(document.getElementById("dg2_" + pre + "_txt_ling").value); //张 色令 txt_ling
var ysP = Number(document.getElementById("dg2_" + pre + "_txt_YsP").value); //张 色 txt_YsP
var seling = Number(document.getElementById("dg2_" + pre + "_TextBox3").value); //色令价 TextBox3
var qibanfei = Number(document.getElementById("dg2_" + pre + "_TextBox7").value); //起版费 TextBox7
var taoban = Number(document.getElementById("dg2_" + pre + "_txt_taoban").value); //套版 txt_taoban

var internalQiBanFei = Number(document.getElementById("dg2_" + pre + "_HidInternalQiBanFei").value); //内部起版费 HidInternalQiBanFei
var internalSeLing = Number(document.getElementById("dg2_" + pre + "_HidInternalSeLing").value); //内部色令价 HidInternalSeLing
if (taoban == 0) {
document.getElementById("dg2_" + pre + "_HidPrintingDanXiang").value = 0; //印刷 内部单项费用
document.getElementById("dg2_" + pre + "_TextBox2").value = 0; //印刷 单项费用 TextBox2

} else {
document.getElementById("dg2_" + pre + "_HidPrintingDanXiang").value = (Math.max(internalQiBanFei, ysling * ysP * internalSeLing)).toFixed(2); //印刷 内部单项费用 Add by Lebron in 2012-1-10
document.getElementById("dg2_" + pre + "_TextBox2").value = (Math.max(qibanfei, ysling * ysP * seling)).toFixed(2); //印刷 单项费用 TextBox2


}
}


------解决方案--------------------
这是不可能的。。。。你页面都没加载出来。。根本就获取不到元素。。会出错的。。
------解决方案--------------------
window.onload=function(){
getYinshua(document.getElementById("文本域的ID"));
}
试试
------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>    
        <style>
        </style>        
    </head>
    <body>
        <select id="test">
            <option value="1">1-1</option>
            <option value="2">1-2</option>
            <option value="3">1-3</option>
        </select>
        <script>
            function $(el){
                return typeof el == 'string' ? document.getElementById(el) : el;
            }
            function test(o){
                alert( o.value )
            }
            $('test').onchange = function(){
                test(this);
            }
            window.onload = function(){
                $('test').onchange()
            }
        </script>
    </body>
</html>

------解决方案--------------------
把 this 换成 一变量

window.onload=function(){
getYinshua(document.getElementById("id").value);
}

------解决方案--------------------
探讨

window.onload=function(){
getYinshua(document.getElementById("文本域的ID"));
}
试试