【发布时间】:2012-03-06 13:18:51
【问题描述】:
我在我的 mvc3 应用程序中编写了一个简单的联系表单。尽管所有字段都无效,但一切似乎都运行良好,除了将焦点放在我的电子邮件字段上无效提交似乎存在问题。这是我正在使用的 ViewModel:
public class ContactViewModel
{
[Required(ErrorMessage="Please enter your name")]
public string Name { get; set; }
[Required(ErrorMessage="Please enter your email address")]
[RegularExpression(@"^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$", ErrorMessage="Please enter a valid email address")]
public string Email { get; set; }
[Required(ErrorMessage="Please enter your message")]
public string Message { get; set; }
}
如您所见,字段之间的唯一区别是我在电子邮件字段中有一个额外的正则表达式验证器。我认为这与为什么它在验证失败时跳转到这个特定字段有关?
这是我的观点的代码:
@model MSExport.Models.ContactViewModel
@{
ViewBag.Title = "Contact";
}
@section HeaderScripts
{
<script type="text/javascript">
$(document).ready(function () {
$('INPUT.auto-hint, TEXTAREA.auto-hint').focus(function () {
if ($(this).val() == $(this).attr('title')) {
$(this).val('');
$(this).removeClass('auto-hint');
}
else { $(this).removeClass('auto-hint'); }
});
$('INPUT.auto-hint, TEXTAREA.auto-hint').blur(function () {
if ($(this).val() == '' && $(this).attr('title') != '') {
$(this).val($(this).attr('title'));
$(this).addClass('auto-hint');
}
});
IterateFields();
$('form').submit(function () {
AmendValues();
if ($(this).valid()) {
var contactVM = {
Name: $('#Name').val(),
Email: $('#Email').val(),
Message: $('#Message').val()
};
$('form').slideUp('slow', function () {
$('#result').html("<img src='/images/loading.gif' alt='Loading' />");
$.ajax({
url: '/Contact/SubmitForm',
type: "POST",
data: JSON.stringify(contactVM),
contentType: "application/json; charset=utf-8",
success: function (result) {
$('#result').empty();
$('#result').html(result);
},
error: function () {
$('#result').empty();
$('#result').html("<p>There was a problem submitting your message. Please try again.</p>");
}
});
});
}
else {
IterateFields();
// TODO: Stop focus going to email field
}
});
});
function AmendValues() {
$('#contact-form-wrapper INPUT[type=text],TEXTAREA').each(function () {
if ($(this).val() == $(this).attr('title')) { $(this).val(''); }
});
}
function IterateFields() {
$('INPUT.auto-hint, TEXTAREA.auto-hint').each(function () {
if ($(this).attr('title') == '') { return; }
if ($(this).val() == '') { $(this).val($(this).attr('title')); }
else { $(this).removeClass('auto-hint'); }
});
}
</script>
}
<h1>Contact</h1>
<div id="result"></div>
@using (Html.BeginForm()) {
<div id="contact-form-wrapper">
<label for="Name">
@Html.TextBoxFor(model => model.Name, new { @class = "auto-hint", @title = "Name", @tabindex = "1", @maxlength = "100" })
@Html.ValidationMessageFor(model => model.Name)
</label>
<label for="Email">
@Html.TextBoxFor(model => model.Email, new { @class = "auto-hint", @title = "Email", @tabindex = "2", @maxlength = "200" })
@Html.ValidationMessageFor(model => model.Email)
</label>
<label for="Message">
@Html.TextAreaFor(model => model.Message, new { @class = "auto-hint", @title = "Message", @tabindex = "3", @maxlength = "2000" })
@Html.ValidationMessageFor(model => model.Message)
</label>
<input type="submit" value="Submit" tabindex="4" id="submit" />
</div>
}
我尝试通过将$('#submit').focus() 放在// TODO: 评论所在的位置来将焦点转移到提交按钮。然而,这并没有解决问题,因为在将焦点转移到提交按钮之前,它似乎仍然将焦点转移到电子邮件字段,这反过来意味着我对该字段的“自动提示”被删除了。
有什么办法可以完全阻止焦点转移?如果没有,我可以在不先转到我的电子邮件字段的情况下以某种方式将其转移到提交按钮吗?
谢谢!
【问题讨论】:
标签: jquery asp.net asp.net-mvc-3