當輸入獲得焦點時,如果輸入無效,我想彈出警告。 該消息是在可觀察的。從輸入字段中查找可觀察值
因此,給定此代碼,我如何找到綁定到它的observable。
$(document).on("focus", "input.invalid", function(){
console.log("ahaaaa!");
//your code here
//dig out observable from this and find the message
//create element with class invalid-message and place it next to this
}).on("blur", function(){
$(".invalid-message").remove();
});
我正在使用基因敲除驗證,我只想在字段有焦點時顯示錯誤消息。歡迎其他建議。
編輯: 當我使用dataFor:
$(document).on("focus", "input.invalid", function(){
console.log(this);
console.log(ko.dataFor(this));
...
我得到這個在控制檯:
下劃線觀察到的是一個我之後。
EDIT2: 我工作圍繞它是這樣的:
$(document).on("focus", "input.invalid", function(){
var fieldName = $(this).attr("name");
var errorMessage = ko.dataFor(this)[fieldName].error;
...
謝謝,但我無法得到該領域的可觀察性。我得到父母(整個表單的可觀測值)。 – Gunslinger
從我看到你得到一個SupplierObservable其中有supplierName obervable?那麼爲什麼你沒有通過以下方式獲得所需的可變長度:var neededObervable = ko.dataFor(this)()。supplierName; –
我可以通過將observable的名稱設置爲字段的名稱來解決此問題。但我希望這不是唯一的方法。 – Gunslinger