2
我在JavaScript中获得了一个类。在这个类中,我有一个方法检查文本字段的输入。当加载html文档的主体时,我想第一次调用这个方法。之后,我想使用“onchange()”事件。JavaScript类中的调用方法
//##############
//# Javascript #
//##############
class NoteController{
constructor() {
this.store = new NoteStore(); // instance of a dataStore
}
HandleInputFields(){ // Enable/Disable a button by validation
var input = document.getElementById('edtNoteTitle').value; // Get the input text from the field
var inputIsValid = true;
if(input.length < 1) // text is empty
inputIsValid = false;
else if (this.store.notes.some(n => n.title === input)) // check for duplicates in the store
inputIsValid = false;
document.getElementById('btnCreateNote').disabled = !inputIsValid; // disable the button or keep it enabled
}
}
//########
//# HTML #
//########
<body onload="HandleInputFields()"> // Disable the button when loading the Document
<input type="text" id="edtNoteTitle" onchange="HandleInputFields()"> // Validate the Input field
</body>
所以当我打开我的文档时,它说“HandleInputFields()”没有被定义。我怎样才能正确调用这个方法?
非常感谢! :)我怎么能通过输入值作为参数? 'document.getElementById('edtNoteTitle')。value'例如'' ? – peterHasemann
youre欢迎,关于第二个问题 - 几乎应该是'onclick =“NoteController.CreateNote(”+ document.getElementById('edtNoteTitle')。value +“)”' – lustoykov
谢谢:)帮助 – peterHasemann