javascript onclick在单击按钮时不触发
我试图在用户单击按钮时调用JS函数。但是onclick事件没有被触发。开发人员工具向我显示以下错误: 执行时出错:javascript onclick在单击按钮时不触发,javascript,onclick,dojo,event-handling,event-binding,Javascript,Onclick,Dojo,Event Handling,Event Binding,我试图在用户单击按钮时调用JS函数。但是onclick事件没有被触发。开发人员工具向我显示以下错误: 执行时出错: function (/*Event*/ e){ // summary: // Handler when the user activates the button portion. if(this._onClick(e) === false){ // returning nothing is same as true
function (/*Event*/ e){
// summary:
// Handler when the user activates the button portion.
if(this._onClick(e) === false){ // returning nothing is same as true
e.preventDefault(); // needed for checkbox
}else if(this.type == "submit" && !this.focusNode.form){ // see if a nonform widget needs to be signalled
for(var node=this.domNode; node.parentNode/*#5935*/; node=node.parentNode){
var widget=dijit.byNode(node);
if(widget && typeof widget._onSubmit == "function"){
widget._onSubmit(e);
break;
}
}
}
}
ReferenceError
arguments: Array[1]
get message: function () { [native code] }
get stack: function () { [native code] }
set message: function () { [native code] }
set stack: function () { [native code] }
type: "not_defined"
__proto__: Error
这是我的密码:
HTML
有什么建议吗?这很有效
function onPing() {
alert("this");
}
可以保持按钮的原样
<button dojoType = "xwt.widget.form.TextButton" id = "pingButton" baseClass = "defaultButton" onclick = "onPing();">Ping</button>
Ping
我将向OP和其他人解释他为什么会遇到问题,而不是继续对表线程进行猛烈的攻击
问题1:OP没有发布所有相关代码。OP对OOP和SO来说显然都是新的,他犯了一个致命的错误,假设少就是多。我们可以看到,根据冒号:
语法,有一个onPing
函数是对象的一部分。如果他发布所有代码,他的javascript将如下所示:
var myObject = {
onPing: function() {
alert("works");
}
};
这里应该指出的是,我不确定他的物体的名字是什么。我使用名称myObject
来暗示“此处的对象名称”
问题2:有一个onclick函数,它是通过按钮内联定义的。这不是好的做法,可能会导致错误。。。尤其是在闭包中编写了如此多的剪切粘贴javascript片段时:
(function() {
/*
This is an example of closure.
Any variables/objects/functions defined in here are not accessible to the outside world
*/
})();
问题3:代码似乎依赖于属性类型
,该属性的值为提交
。因此,按钮应设置该属性:
<button type = "submit" dojoType = "xwt.widget.form.TextButton" id = "pingButton" baseClass = "defaultButton" onclick = "onPing();">Ping</button>
为了更好,我们在您的
中添加了一个。添加type=“button”
,因为代码会检查type
属性。即使在将类型添加到我的中之后,同样的错误也会出现。。。还应该说type=“submit”
,尽量不要使用onclick属性。尝试在代码中绑定事件:document.getElementById(“pingButton”).onclick=onPing代码>。您的onPing函数似乎也是对象的一部分。在这种情况下,您需要完全限定函数名:myObject.onPing
,该函数名不能解决问题,但提供了不同的解决方案。代码导致了一个错误,这就是他需要修复的地方。@RyanWheale对此表示抱歉。我本以为这没什么区别……没必要道歉。我讨厌投否决票:(
-但是当一个解决方案的目的不是解决问题时,我觉得社区有责任将其公之于众。你能解释一下为什么你的答案可能会引导他找到解决方案吗?如果是,我会收回它。@RyanWheale我提出这个解决方案的原因是因为我将代码放入VS并运行了它,FireBug给了我一个JS错误sayingSyntaxError:function语句需要在ping:function()上使用名称[Break On This Error]{…
因此,我把它改成了我的答案,它成功了。顺便说一句,你确定你的解决方案有效吗。当我按上面提供的按钮,然后添加提交建议时,我仍然得到相同的错误…:我没有提供解决方案…但是如果你是指我上面的评论…我提到他的正在使用的事实>函数似乎是一个对象的成员…我告诉他完全限定函数名:myObject.onPing
。这很可能会解决他的问题。定义一个在某个范围内浮动的错误函数(正如您所建议的)不是解决方案,只会导致更多问题。
(function() {
/*
This is an example of closure.
Any variables/objects/functions defined in here are not accessible to the outside world
*/
})();
<button type = "submit" dojoType = "xwt.widget.form.TextButton" id = "pingButton" baseClass = "defaultButton" onclick = "onPing();">Ping</button>
<button type="submit" dojoType="xwt.widget.form.TextButton" id="pingButton" baseClass="defaultButton">Ping</button>
<script>
(function() {
var myObject = {
onPing: function() {
alert("works");
}
};
document.getElementById('pingButton').onclick = myObject.onPing;
}());
</script>