这是我的登录视图:
@model SuburbanCustPortal.Models.LogOnModel
@{
ViewBag.Title = "Log On";
}
<h2>Log On</h2>
<p>
Please enter your user name and password. @Html.ActionLink("Register", "Register") if you don't have an account.
</p>
<p>
If only you wish to make a payment on your account and do not want to create a website login, @Html.ActionLink("click here", "RedirectToPaymentPage", "Account").
</p>
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field focus">
@Html.TextBoxFor(m => m.UserName, new { @class = "GenericTextBox", onkeyup = "enableLogonButton()" })
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password, new { @class = "GenericPasswordBox", onkeyup = "enableLogonButton()" })
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<p>
<button name="btn" value="Log On" onclick="disableButton()" disabled="disabled">Log On</button>
</p>
<p>
If you need to retrieve your username @Html.ActionLink("click here.", "ForgotUsername", "Account")<br/>
If you need to reset your password @Html.ActionLink("click here.", "ForgotPassword", "Account")
</p>
</fieldset>
</div>
}
这是我的 widget.js:
function enableLogonButton() {
if ($('#UserName').val() == "") {
$('#btn').attr('disabled', 'disabled');
return;
}
if ($('#Password').val() == "") {
$('#btn').attr('disabled', 'disabled');
return;
}
$('#btn').removeAttr('disabled');
}
function logonDisableSubmitButtons(clickedButton) {
$("input[type='button']").each(function() {
if (this.name != clickedButton)
$(this).attr("disabled", "disabled");
else {
//hiding the actually clicked button
$(this).hide();
//Creating dummy button to same like clicked button
$(this).after('<input type="button" disabled="disabled" value="' + $(this).val() + '" class="' + $(this).attr('class') + '" />');
}
});
}
function disableButton() {
$('#btn').click(function (e) {
$(this).attr('disabled', 'disabled');
});
}
无论我做什么,我都没有启用按钮。
我想要的是两件事:
- 当用户在登录名和用户名中有内容时,启用提交按钮。
- 当他们单击提交时,该按钮被禁用,因此他们无法单击两次。
我也没有运气。
我已经进行了下面建议的更改,但是一旦我在字段中输入了某些内容,该按钮仍未启用
** 进一步测试**
我按照以下建议添加了警报():
function enableLogonButton() {
alert("start");
if ($('#UserName').val() == "") {
alert("username");
$('#btn').attr('disabled', 'disabled');
return;
}
if ($('#Password').val() == "") {
alert("password");
$('#btn').attr('disabled', 'disabled');
return;
}
alert("enabled");
$('#btn').removeAttr('disabled');
}
他们中没有一个正在开火。至少,我没有得到任何提示。
所以,然后我将调用更改为 this 以确保它甚至可以看到 JScript:
@Html.PasswordFor(m => m.Password, new { @class = "GenericPasswordBox", onkeyup = "enableLogonButtonX()" })
我收到这条消息:
Microsoft JScript 运行时错误:“enableLogonButtonX”未定义
所以,我相信它正在看到 JScript。
然后我添加了这个:
<script>
function myFunction() {
alert("hit!");
}
</script>
并改变了这一点:
<button name="btn" value="Log On" onclick="myFunction()" disabled="disabled">Log On</button>
我的“命中”奏效了。所以,我相信 onclick 也有效。