简体   繁体   English

提交按钮上的Java语言

[英]Unobtrusive Javascript on a Submit Button

I'm trying to write an unobtrusive function on a submit button without the use of jQuery and the like. 我正在尝试在不使用jQuery之类的情况下在提交按钮上编写不引人注目的功能。 I have the following bit of code in my HTML form: 我的HTML表单中包含以下代码:

document.getElementById('help_submit').onclick = function (){
    window.alert("You clicked on the Submit Button on the Help Request form.");
};

And I'm trying to use on the following HTML button: 我正在尝试在以下HTML按钮上使用:

<input type="submit" id="help_submit" value="Submit Help Request" />

However when I try to fire the event, the form doesn't pop up with the Message Box and submits anyway. 但是,当我尝试触发该事件时,该窗体不会随消息框一起弹出,并且仍然会提交。

I check the developer tools in Chrome and I see the following error: 我检查了Chrome中的开发者工具,并看到以下错误:

Uncaught TypeError: Cannot set property 'onclick' of null

Where did I go wrong with the coding? 我在哪里弄错了编码?

It seems likely that you are running your Javascript too early before the DOM has loaded and thus document.getElementById('help_submit') does not find the DOM element because it is not yet in the page. 您似乎在DOM加载之前就运行Java document.getElementById('help_submit') ,因此document.getElementById('help_submit')找不到DOM元素,因为它尚未在页面中。 You can fix this by moving the script that contains this code to right before the </body> tag so all DOM elements will be present when the script runs. 您可以通过将包含此代码的脚本移到</body>标记之前的位置来解决此问题,以便在脚本运行时所有DOM元素都存在。 For more details on this issue, consult this answer: pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it . 有关此问题的更多详细信息,请参考以下答案: 纯JavaScript等效于jQuery的$ .ready()如何在页面/ dom准备就绪时调用函数

In addition, your submit handling code needs to prevent the default action of the submit button or the form will submit anyway. 另外,您的提交处理代码需要防止“提交”按钮的默认操作,否则表单仍然会提交。 Though, if you don't want the form to submit, then you should just stop using a submit button and use a regular button. 但是,如果您不希望提交表单,则应该停止使用“提交”按钮,而使用常规按钮。

In addition to moving the script to the end of the DOM, I'd suggest you change your event handling code to this: 除了将脚本移到DOM的末尾,我建议您将事件处理代码更改为此:

document.getElementById('help_submit').addEventListener('click', function (e) {
    e.preventDefault();
    window.alert("You clicked on the Submit Button on the Help Request form.");
});

Working demo: http://jsfiddle.net/jfriend00/vcjtv0kz/ 工作演示: http : //jsfiddle.net/jfriend00/vcjtv0kz/

I'm not experienced in JavaScript, but, following on the comment and the answer already given, try changing your code the following way: 我没有JavaScript方面的经验,但是在给出注释和答案后,请尝试通过以下方式更改代码:

  1. Remove the given code (it will be used differently at the next steps). 删除给定的代码(在以后的步骤中将以不同的方式使用它)。

  2. Inside the script tag inside the head element, try creating two functions called, say, initialization and message: 在head元素内的script标签内,尝试创建两个函数,例如,初始化和消息:

     function initialization() { element = document.getElementById('help_submit'); element.addEventListener( 'click', message, false ); } function message() { window.alert("You clicked on the Submit Button on the Help Request form."); } 
  3. At the end of this script tag, write the following: 在此脚本标签的末尾,编写以下内容:

     window.addEventListener( "load", initialization, false ); 

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

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