如何禁用该按钮的按钮和 Jquery 代码 - asp.net mvc-3
Posted
技术标签:
【中文标题】如何禁用该按钮的按钮和 Jquery 代码 - asp.net mvc-3【英文标题】:how to disable button and Jquery code for that button - asp.net mvc-3 【发布时间】:2011-11-10 04:31:27 【问题描述】:似乎还有另一个问题..不是 JQuery 代码而是 Action 控制器代码...
我将断点放在控制器上并开始调试应用程序,它会立即进入控制器并尝试传入空值...请看一下并告诉我...谢谢
public class HomeController : Controller
//
// GET: /Home/
UsersRepository repo = new UsersRepository();
DBHelpers db = new DBHelpers();
public ActionResult Index()
var users = new Users();
return View(users);
public ContentResult CheckLogin(Users checkuser)
checkuser = new Users();
if (db.CheckUserLoginDetails(checkuser))
return Content("Login Successful:" + checkuser.Email);
else
return Content("Login UnSuccessful");
public ContentResult Register(Users userRegister)
userRegister = new Users();
if (db.InsertNewUSerDetails(userRegister))
return Content("Registration Successful : your ID is : " + userRegister.UserID);
else
return Content("There was a problem addding you, please try again");
==================
这是我的 DBHelpers 课程
UsersRepository db = new UsersRepository();
public bool CheckUserLoginDetails(Users user)
return (from u in db.EUsers where u.Email == user.Email & u.Password == user.Password select u).Any();
public bool InsertNewUSerDetails(Users newUser)
newUser = new Users();
try
var date = DateTime.Now.Date;
newUser.JoiningDate = date;
db.EUsers.Add(newUser);
db.SaveChanges();
return true;
catch (DbEntityValidationException dbEX)
foreach (var validationerrors in dbEX.EntityValidationErrors)
foreach (var valerror in validationerrors.ValidationErrors)
Trace.TraceInformation("Property: 0 Error: 1", valerror.PropertyName, valerror.ErrorMessage);
return false;
这是我的用户类
public class Users
[Key]
public virtual int UserID get; set;
[Required(ErrorMessage="Required")]
public virtual string FirstName get; set;
[Required(ErrorMessage = "Required")]
public virtual string LastName get; set;
[Required(ErrorMessage = "Required")]
[DataType(DataType.EmailAddress)]
public virtual string Email get; set;
[Required(ErrorMessage = "Required")]
[DataType(DataType.Password)]
public virtual string Password get; set;
[DataType(DataType.Date)]
public virtual DateTime JoiningDate get; set;
=============== 问题是当我尝试运行应用程序时,它直接进入 Register() 操作并传递空值...
请帮忙...
==================
这是我的查看代码...
@model Temp1.Models.Users
@
ViewBag.Title = "Index";
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js") type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.5.1-vsdoc.js") type="text/javascript"></script>
@using (html.BeginForm())
<fieldset>
<div id="divLoginInfo">
<p>if not registered, please @Html.ActionLink("Register", "Register", "Home", new @id = "lnkRegister" )</p>
@Html.TextBoxFor(model => model.Email, new @class = "text" )
<br />
@Html.PasswordFor(model => model.Password, new @class = "text" )
<p>
<input type="button" value="Login" id="btnLogin" />
</p>
<div id="Result">
<div class="Loading" style="display:none;"> Checking....</div>
</div>
</div>
</fieldset>
<br />
<div id="divRegisterInfo" style="display:none;">
<fieldset>
<legend>Users</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<p>
<input type="button" value="Create" id="btnRegister" />
</p>
<p>
<div id="divShowRegistrationMessage">
</div>
</p>
</fieldset>
</div>
<script type="text/javascript">
$('#btnLogin').click(function (e)
var email = $('#Email').val();
var Password = $('#Password').val();
var postdata =
"Email": email,
"Password": Password
;
$('.Loading').fadeIn(50);
$.ajax(
url: '@Url.Action("CheckLogin","Home")',
data: postdata,
success: function (msg)
var data = msg.split(':');
$('#Result').html(data[0]);
$('.Loading').fadeOut(50);
,
error: function (data)
$('#Result').html(data);
$('.Loading').fadeOut(50);
);
e.preventDefault();
);
$('#lnkRegister').click(function (e)
e.preventDefault();
$('#divLoginInfo').fadeOut(100);
$('#divRegisterInfo').fadeIn(100);
);
$('#btnRegister').click(function (e)
var firstName = $('#divRegisterInfo #FirstName').val();
var lastName = $('#divRegisterInfo #LastName').val();
var email = $('#divRegisterInfo #Email').val();
var password = $('#divRegisterInfo #Password').val();
var postdata =
"FirstName": firstName,
"LastName": lastName,
"Email": email,
"Password": password
;
$("#divShowRegistrationMessage").html('Pleaes wait...');
e.preventDefault();
$.ajax(
type: 'POST'
url: '@Html.Action("Register","Home")',
data: postdata,
success: function (msg)
$('#divShowRegistrationMessage').html(msg);
,
error: function (data)
$('#divShowRegistrationMessage').html(data);
);
);
</script>
【问题讨论】:
您需要重新表述您的问题,并包含更多代码,以便任何人都能够提供帮助 您好,请查看更新后的代码...并告诉我?... 【参考方案1】:您在此处有一个尾随 ,
,这可能会导致 javascript 错误:
var postdata =
"FirstName": firstName,
"LastName": lastName,
"Email": email,
"Password": password, // <-- remove this comma
;
javascript 错误导致下一行未执行,即e.preventDefault();
,因此表单执行正常提交而不是 AJAX 提交。 IIRC 某些浏览器实际上可能会容忍这一点,但其他浏览器则不会。无论如何,这是无效的javascript,需要修复。
【讨论】:
嗨,我已经删除了...仍然调用 btnLogin 控制器操作 ....我不知道出了什么问题... @patel.milanb,也许还有其他一些我没有看到的 javascript 错误?如果您在函数末尾的 $.ajax 调用之后发出警报,它会被执行吗? 事实上,现在我仔细观察,我在您的代码中看到了这个alert(joiningdate);
,但看不到这个joiningdate
变量的定义位置。那么它是在某个地方定义的吗?您还可以查看 FireBug 的 Console 选项卡,该选项卡会显示您的 javascript 错误,您无需提出此类问题。
嗨,我已经这样做了,但现在似乎还有另一个问题......当我试图调试应用程序并且我也在我的动作控制器上放置断点......它直接调用控制器,它正在传递空值,因此 JQuery 中出现错误......请帮我看看控制器代码,让我知道...... thx
@patel.milanb,发送到服务器的 AJAX 请求在 FireBug 中是什么样子的?究竟是什么发送到服务器?【参考方案2】:
确保为每个按钮附加不同的点击事件。
如果你目前正在做:
$('div').click(function());
然后你会想要为每个按钮附加一个不同的点击事件,比如:
$('#registerButtonId').click(function());
$('#loginButtonId').click(function());
【讨论】:
嗨,谢谢...请查看更新的问题和代码...让我知道我哪里出错了...以上是关于如何禁用该按钮的按钮和 Jquery 代码 - asp.net mvc-3的主要内容,如果未能解决你的问题,请参考以下文章
教用jQuery给按钮添加disabled属性,该按钮禁用后,点击该按钮仍可以触发事件!求大神帮忙! 急,在线等!
如何通过单击按钮禁用 JQuery 功能,但默认保持打开状态?