因此,我遵循了Dojo中的示例 - 使用Dojo JavaScript庫構建Ajax應用程序,以將服務器端驗證添加到窗體上的用戶名validationtextbox字段。基本上我添加了一個提交xhrGet請求的usernameOnChange函數,xhrGet返回JSON並由usernameValidationHandler處理。dojo dijit客戶端驗證onchange
它工作的很好,但usernameValidationHandler只設置工具提示顯示消息爲錯誤。它不會將字段設置爲無效,因此用戶仍然可以提交表單。如何將字段設置爲無效,以便表單不會提交?
<input type="text" id="userName" name="userName" size="20"
dojoType="dijit.form.ValidationTextBox"
trim="true"
required="true"
onChange="userNameOnChange"
regExp="\w+"
invalidMessage="User name is required"
/>
function userNameOnChange() {
var userName = dijit.byId("userName").getValue();
if (userName == "") {
return;
}
dojo.xhrGet({
url: "validateUserName.jsp?userName=" + userName,
handleAs: "json",
handle: userNameValidationHandler
});
}
function userNameValidationHandler(response) {
dijit.byId("userName").displayMessage();
if (!response.valid) {
var errorMessage = "User name already taken";
// Display error message as tooltip next to field
dijit.byId("userName").displayMessage(errorMessage);
// HOW DO I SET THE FIELD TO BE INVALID NOW???
}
}
您可以使用dojo.connect將小部件中的'onChange'方法直接連接到將保留'this '參考。這樣你就不必進行id查找並鎖定到一個小部件實例。 – peller