但还有更多,所以我尝试调试这个,在谷歌的Chrome浏览器中我使用控制台,我键入类似$(“#myform”)。validate()。element(“#myselect”);逐个检查每个文本框,并且它们都返回true,这意味着我的输入已经过验证并通过了测试,但是当我运行应用程序并单击我的添加按钮时,表单不是有效的,有什么问题?
$(function () {
$(document).on("click", ".btnAddSongTilesToGenre", function (e) {
var name = $('#youTubeNameTxt').val();
var link = $('#youTubeLinkTxt').val();
var len = link.length;
var substr = link.substr(31, len - 31);
var container = $(this).parent().parent().find(".actualTilesContainer");
$(container).slideDown();
var genreId = $(this).attr("name");
$("#hiddenRank").val(genreId);
$("#AddTopTenFavForm").validate().form();
if ($("#AddTopTenFavForm").valid()) {
$.ajax({
beforeSend: function () { ShowAjaxLoader(); },
url: "/Home/AddSong",
type: "POST",
data: $("#AddTopTenFavForm").serialize(),
success: function (data) { HideAjaxLoader(), ShowMsg("Song Added Successfully"), $(container).find('ul').append('<li><a class="savedLinks" href="#" name="' + substr + '" >' + name + '</a> <span name= ' + data + ' class="btnDeleteSong dontDoAnything">x</span></li>'); },
error: function () { HideAjaxLoader(), ShowMsg("Song could not be added, please try again") }
});
$('#youTubeLinkTxt').val('');
$('#youTubeNameTxt').val('');
}
else {
alert("notValid");
}
if ($(e.target).hasClass("dontDoAnything")) {
e.stopPropagation();
return false;
}
});
$("#otherFavContainer").on("click", ".songTilesGenreContainer", function (e) {
var myVar = $(this).find(".actualTilesContainer");
if ($(myVar).hasClass("minimalized")) {
$(myVar).removeClass("minimalized").addClass("maximized").slideDown();
}
else {
$(myVar).removeClass("maximized").addClass("minimalized").slideUp();
}
});
我的验证属性
namespace yplaylist.Models
{
[MetadataType(typeof(TopTenFav_Validation))]
public partial class TopTenFav
{
}
public class TopTenFav_Validation
{
[RegularExpression("http://www.youtube.com/watch\\?v=.*", ErrorMessage = "Youtube Link must begin with: http://www.youtube.com/watch?v= ")]
[Required(ErrorMessage = "Youtube link is Required")]
[StringLength(100, ErrorMessage = "Song Title cannot exceed 100 characters")]
public string YoutubeLink { get; set; }
[StringLength(100, ErrorMessage = "Youtube link cannot exceed 100 characters")]
[Required(ErrorMessage = "Song title is Required")]
public string Title { get; set; }
}
}