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