我已经在html文件里面写了下面的脚本。
我在firstNameInputEditor类中附加了带有firstName.change函数的文本框的更改事件和一个update函数,这是我在创建firstNameInputEditor类的实例后扩展的。
现在的问题是,当调用change事件时,我的update函数没有被调用。
我想创建不同的firstNameInputEditor实例,它可以有不同的更新功能实现。并且这个更新函数应该从change函数内部调用。
热心的帮助
<html>
<head>
<title>
User Resgistration for
</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
</head>
<body>
<form id ="myForm">
First Name <input type="text" id = "firstName" onchange = "firstName.change(event)"/></br>
</form>
<script type="text/javascript">
var firstNameInputEditor = Class.create({
initialize: function(id) {
this.elementId = id;
this.elementId.onchange = this.change;
},
update :function(event){
console.log('outside update', event);
},
change : function(event){
this.update(event);
console.log('change update', event);
}
});
var firstName = new firstNameInputEditor($('firstName'));
firstName.update = function(event){
console.log('new update' + event);
}
</script>
</body>
</html>
发布于 2016-10-15 12:36:28
好的,看起来你还没有添加所需的库。
添加以下脚本代码:
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
下面是这方面的工作代码-
JS小提琴- https://jsfiddle.net/vikash2402/ce75kdr9/3/
var firstNameInputEditor = Class.create({
initialize: function(id) {
this.elementId = id;
this.elementId.onchange=this.change;
},
update :function(event){
},
change : function(event){
this.update(event);
alert("value changed");
console.log(event);
}
});
var firstName = new firstNameInputEditor($('firstName'));
firstName.update =function(event){
console.log('new update' + event);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
<body>
<form id ="myForm">
First Name <input type="text" id = "firstName" onchange = "firstName.change(event)"/></br>
</form>
</body>
希望这能对你有所帮助:)
发布于 2016-10-15 14:21:17
设法找到了答案。在初始化函数中,我存储了正确的引用,并使用它从change函数调用update函数
<html>
<head><title> User Resgistration form</title></head>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
<body>
<form id ="myForm" actiion="\resgistrationFormAction.jsp">
First Name <input type="text" id = "firstNameId" /></br>
</form>
</body>
<script type="text/javascript">
var firstNameInputEditor = Class.create({
initialize: function(id) {
this.elementId = id;
this.elementId.onchange=this.change;
that= this;
},
update :function(event){
},
change : function(event){
that.update(event);
console.log(event);
}
});
var firstName = new firstNameInputEditor($('firstNameId'));
firstName.update =function(event){
console.log('new update' + event);
}
</script>
</html>
https://stackoverflow.com/questions/40058928
复制相似问题