简体   繁体   English

调用时函数未运行

[英]Function Not Running When Called

I have run into what I think is a rather simple problem, but cannot see the error. 我遇到了我认为是一个非常简单的问题,但是看不到错误。 I have two functions that I am trying to call (one to display the date and another get focus on a text input box). 我尝试调用两个函数(一个显示日期,另一个聚焦在文本输入框上)。 When I run the web page nothing happens, so I am stumped. 当我运行网页时,什么都没有发生,所以我很困惑。

I have created a fiddle here: http://jsfiddle.net/dk9D9/ 我在这里创建了一个小提琴: http : //jsfiddle.net/dk9D9/

And this is the specific javascript that I am working with. 这是我正在使用的特定javascript。

var $ = function(id) { return document.getElementById(id); }

function todayTxt() {
   var today = new Date();
   return today.getMonth() + 1 + "-" + today.getDate() + "-" + today.getFullYear();
}

var initForm = function ()
{
    document.getElementById('qty1').focus()
}

window.onload = function () {
    $(initForm);
    $(todayTxt);
}

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You have defined initForm function as a variable. 您已将initForm函数定义为变量。 You should call it with: 您应该使用以下方式调用它:

initForm();

The same thing about todayTxt : 关于todayTxt同样的事情:

todayTxt();

$ function returns an element by given ID string as a parameter. $函数通过给定的ID字符串作为参数返回元素。 The valid usage of it is: 它的有效用法是:

$('elementId')

Valid code can looks like: 有效代码如下所示:

var $ = function(id) { return document.getElementById(id); }

function todayTxt() {
   var today = new Date();
   return today.getMonth() + 1 + "-" + today.getDate() + "-" + today.getFullYear();
}

var initForm = function() {
    $('qty1').focus();
}

window.onload = function() {
    initForm();
    todayTxt();
}

My guess is that you're not calling the function. 我的猜测是您没有调用该函数。

$(something); 

is retrieving something , but not calling it as a function. 正在检索something ,但未将其作为函数调用。

Why are you using $(something) instead of calling initform(); 为什么使用$(something)而不是调用initform(); as a regular function? 作为常规功能?

Following the call chain from the entry-point ( window.onload ), you are only actually calling 2 functions: 从入口点( window.onload )遵循调用链,实际上您只在调用两个函数:

  1. Your anonymous function for onload . 您的onload匿名函数。
  2. The $ function. $函数。

You have defined $ as document.getElementById(id) . 您已将$定义为document.getElementById(id) When you call $ , you are passing references to the initForm and todayTxt function into $ as the id parameter. 调用$ ,您initForminitFormtodayTxt函数的引用作为id参数传递到$中。

In otherwords, when you call 换句话说,当您致电

$(initForm)

when entering $, we are calling 输入$时,我们正在调用

document.getElementById(initForm);

Since neither function has anything to do with $ , you probably are looking for: 由于这两个函数都不与$ ,因此您可能正在寻找:

window.onload = function () {
    initForm();
    todayTxt();
}

Seems like JSFiddle was calling onload on the JS input section so setting another onload function was preventing the code from running so I disabled it and made the necessary changes to make the page function. 好像JSFiddle在JS输入部分上调用onload ,因此设置另一个onload函数会阻止代码运行,因此我禁用了它并进行了必要的更改以使页面功能。

http://jsfiddle.net/keWD5/ http://jsfiddle.net/keWD5/

(You will need the window.onload call in your actual page.) (您将需要在实际页面中调用window.onload 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM