【问题标题】:run function when page loads页面加载时运行函数
【发布时间】:2014-02-07 08:55:25
【问题描述】:

我想在页面加载时调用该函数以显示 m2 和 totalpris 中的值,但我的 document.onlods() 杀死了这些函数。不知道我做错了什么(因为我不懂javascripts actuallay :)) 但该功能在没有我的 onload 行的情况下工作

<input id="bredd" type="text" value="2" /> 
<input id="djup" type="text" value="3" />
<input id="m2" type="text" value="" />
<input id="m2pris" type="text" value="500" />
<input id="totalpris" type="text" value="" />

<script>
$(document).ready(function(){
    document.onload=ready(document.getElementById('m2'));
    document.onload=ready(document.getElementById('totalpris'));

    $('#bredd').keyup(calculate);
    $('#djup').keyup(calculate);
    $('#m2pris').keyup(calculate);
});

function calculate(e)
{
    $('#m2').val($('#bredd').val() * $('#djup').val());
    $('#totalpris').val($('#m2').val() * $('#m2pris').val());
}
</script>

【问题讨论】:

  • ready() 是做什么的?你在 DOM 中调用的函数准备好了
  • $(document).ready() 当DOM被加载或$(window).load() 当所有元素被加载(包括脚本/img)
  • window.onload = function() { calculate() }

标签: javascript jquery forms input


【解决方案1】:
$.ready() 

说明:指定DOM完全加载时执行的函数。

$(document).ready() {
    calculate();
    //and anything else
}

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多