简体   繁体   English

基本的Javascript:onclick事件不起作用

[英]Basic Javascript: onclick event not working

Given this simple javascript function: 鉴于此简单的javascript函数:

function update_cart() {
alert("--update--");
document.form1.command.value="update";
document.form1.submit();
}

and this button at the bottom of my form: 和这个按钮在我的表单的底部:

<input type="submit" value="Update" onclick="update_cart()" />

The data in the form is 'submitted' to the new URL. 表格中的数据已“提交”到新的URL。 but the update_cart function is never called. 但从不调用update_cart函数。 I know because I never get an alert box, and the URL reads...?c=Albania&m=.... 我知道,因为我从未收到警报框,并且URL读取...?c = Albania&m = ....

Also, the form element 另外,表单元素

<input type="hidden" name="command"/>

does not get posted to the URL, either. 也不会发布到URL。 URL reads ?command=&c=Albania... URL读取?command =&c = Albania ...

I have tried the following: changed onclick to onsubmit, checking $_REQUEST variables, cutting and pasting the code from known working pages. 我尝试了以下操作:将onclick更改为onsubmit,检查$ _REQUEST变量,从已知工作页面剪切并粘贴代码。

I'm at my wit's end, and would be grateful for any help! 我的智慧到此为止,我们将不胜感激!

Oh, yes: same behaviour in firefox 6, Opera 11.5, & IE7. 哦,是的:firefox 6,Opera 11.5和IE7中的行为相同。 I'm running WinXP SP3. 我正在运行WinXP SP3。

Thanking you, Sadhu! 谢谢你,萨杜!

If you want the script to run on submit of form, use: 如果您希望脚本在提交表单时运行,请使用:

<input type="submit" value="Update" onsubmit="update_cart()" />

The onsubmit event is usually used to run some validation script. onsubmit事件通常用于运行某些验证脚本。 If the validation returns true , the form will submit. 如果验证返回true ,则将提交表单。 If it returns false , the form does not submit. 如果返回false ,则不提交表单。 In your case, the script is coded to submit the form so no return boolean value is necessary. 在您的情况下,脚本被编码为提交表单,因此不需要返回布尔值。

Otherwise, I would not give your button the submit type if it really doesn't submit the form. 否则,如果确实不提交表单,则不会为您的按钮submit类型。 You can simply use button tags with the onclick and that should work: 您可以在onclick简单地使用button标签,它应该可以工作:

<button onclick="update_cart()">Update</button>

Once try 一旦尝试

<input type="button" value="Update" onclick="update_cart()" />

instead 代替

<input type="submit" value="Update" onclick="update_cart()" />

if you want that for 'submit' type then go with 'Allen Liu' answer 如果您要使用“提交”类型,请选择“刘艾伦”答案

enter code here if you wane to change sth when onsubmit, you need to do these changes before form's submitting. 如果您想在提交时更改某项内容,请enter code here ,您需要在提交表单之前进行这些更改。 so you need to add these opeartion to the "onsubmit" event of the form, rather than the "onclick" event of the submit button. 因此,您需要将这些外观添加到表单的“ onsubmit”事件中,而不是“提交”按钮的“ onclick”事件中。

like this: 像这样:

<form name="toSubmit" onsubmit="update_cart();"><input type="submit" name="btn" value="hello"/></form>

First of all there is no need to change your html ; 首先,无需更改您的html see my demo . 看我的演示
On every click on a submit button, first the click-handler (if exists) will be started, then the submit-handler (if exists) (which should be in the form tag) and then the action of the form will be executed. 每次单击提交按钮时,首先将启动单击处理程序(如果存在),然后将启动提交处理程序(如果存在)(应在form标记中),然后将执行表单的action This procedure will be only stoped, if a handler returns false . 仅当处理程序返回false ,此过程才会停止。
But why will your javascript function update_cart not be called? 但是,为什么不调用您的JavaScript函数update_cart
I think it could not be found, but I don't why. 我认为找不到它,但我不为什么。 Can you bind the function to the window dom element only for testing (like in my demo )? 您可以仅将功能绑定到window dom元素上进行测试(例如在我的演示中 )吗?

Ps: you don't need to submit the form in your click-handler (if you don't return false). 附:您无需在点击处理程序中提交表单(如果您未返回false)。 You can remove the line: document.form1.submit(); 您可以删除以下行: document.form1.submit(); .

Ps: it will be better not to use a click-handler on the submit-button, instead use a submit-handler in the form tag (see my demo2 ). 附:最好不要在“提交”按钮上使用单击处理程序,而应在form标记中使用一个提交处理程序(请参阅我的demo2 )。

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

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