Jquery使用生成的id进行validation

我创建了一个表,它将对汽车注册进行AJAX搜索并将信息返回到表中。

然后,这将添加一个带有文本框的新行。 每个文本框都有一个唯一的名称和id,例如[0] .r_make / [1] .r_model一切都正常工作,除非我点击它没有validation的按钮。

会发生什么,它会使用空字段发布下一行。 关于如何validation这一点,我的想法已经不多了。 一些帮助将不胜感激。

HTML:

 
Enter Car Registration
Reg Make Model CC Value Owner year Delete
delete

jQuery的:

 $(function(){ $("input[type$='text']").valid(); }); $(function () { $('#loading').hide() .ajaxStart(function () { $(this).show(); }).ajaxStop(function () { $(this).hide(); }); $("#make-form").hide(); $("#model-form").hide(); var currentID = 0; $("#btnAddVehicle").click(function () { if ($("input[type$='text']").val().length < 1) { // if the input type has no value add message $('#append').html("
Please fill in all fields
"); } if ($("input[id$='[0].r_reg']").val().length < 1){ // if the input reg has no value then submit to it $("input[id$='[0].r_reg'").val($('#vehicle_reg').val()); $.ajax({ //ajax request dataType: "xml", cache: false, type: "GET", url: 'http://localhost/Reg%20Lookup/ajax_asp.asp', data: $('#find_reg').serialize(), success: function (xml) { $(xml).find('VEHICLE').each(function () { $("input[id$='[0].r_make'").val($(this).find('MAKE').text()); $("input[id$='[0].r_model'").val($(this).find('MODEL').text()); $("input[id$='[0].r_cc'").val($(this).find('CC').text()); $("input[id$='[0].r_year'").val($(this).find('MANUF_DATE').text()); }) }, error: function () { // on error alert this message alert("lookup couldnt find your registration, Please fill in all required box's !"); } }) $(".model").click(function () { if ($("input[id$='[" + currentID + "].r_model'").val().length max || o.val().length < min) { o.addClass("ui-state-error"); updateTips("Length of " + n + " must be between " + min + " and " + max + "."); return false; } else { return true; } } function checkRegexp(o, regexp, n) { if (!(regexp.test(o.val()))) { o.addClass("ui-state-error"); updateTips(n); return false; } else { return true; } } $("#model-form").dialog({ autoOpen: false, resizable: true, bgiframe: true, height: 290, width: 450, modal: true, buttons: { "Add Model": function () { var bValid = true; allFields.removeClass("ui-state-error"); bValid = bValid && checkLength(model, "Customer Notes", 1, 2000); if (bValid) { $("input[id$='[" + currentID + "].r_model'").val(model.val()); $(this).dialog("close"); } }, Cancel: function () { $(this).dialog("close"); } }, close: function () { allFields.val("").removeClass("ui-state-error"); } }); } }); $(".make").click(function () { if ($("input[id$='[" + currentID + "].r_make'").val().length max || o.val().length 1) // if the input "reg" has a value then add the row { $("input[id$='[" + currentID + "].r_reg'").attr("disabled", "disabled"); $("input[id$='[" + currentID + "].r_make'").attr("disabled", "disabled"); $("input[id$='[" + currentID + "].r_model'").attr("disabled", "disabled"); $("input[id$='[" + currentID + "].r_cc'").attr("disabled", "disabled"); $("input[id$='[" + currentID + "].r_value'").attr("disabled", "disabled"); $("input[id$='[" + currentID + "].r_owner'").attr("disabled", "disabled"); $("input[id$='[" + currentID + "].r_year'").attr("disabled", "disabled"); $('#append').html('
If box is empty, please click on and select relivent vehicle information.
') currentID++; var htmlToAppend = ""; htmlToAppend += ""; htmlToAppend += ""; htmlToAppend += ""; htmlToAppend += ""; htmlToAppend += ""; htmlToAppend += ""; htmlToAppend += "Delete"; $("#regTable").prepend(htmlToAppend); $("#regTable tr:eq(1)").css('background-color', '#990000') $("#regTable tr:eq(2)").css('background-color', '#cccccc') $("input[id$='[" + currentID + "].r_reg'").val($('#vehicle_reg').val()); $.ajax({ dataType: "xml", cache: false, type: "GET", url: 'http://localhost/Reg%20Lookup/ajax_asp.asp', data: $('#find_reg').serialize(), success: function (xml) { $(xml).find('VEHICLE').each(function () { $("input[id$='[" + currentID + "].r_make'").val($(this).find('MAKE').text()); $("input[id$='[" + currentID + "].r_model'").val($(this).find('MODEL').text()); $("input[id$='[" + currentID + "].r_cc'").val($(this).find('CC').text()); $("input[id$='[" + currentID + "].r_year'").val($(this).find('MANUF_DATE').text()); }) }, error: function () { alert("lookup couldnt find your registration, Please fill in all required box's !"); } }); $(".model").click(function () { if ($("input[id$='[" + currentID + "].r_model'").val().length max || o.val().length < min) { o.addClass("ui-state-error"); updateTips("Length of " + n + " must be between " + min + " and " + max + "."); return false; } else { return true; } } function checkRegexp(o, regexp, n) { if (!(regexp.test(o.val()))) { o.addClass("ui-state-error"); updateTips(n); return false; } else { return true; } } $("#model-form").dialog({ autoOpen: false, resizable: true, bgiframe: true, height: 290, width: 450, modal: true, buttons: { "Add Model": function () { var bValid = true; allFields.removeClass("ui-state-error"); bValid = bValid && checkLength(model, "Customer Notes", 1, 2000); if (bValid) { $("input[id$='[" + currentID + "].r_model'").val(model.val()); $(this).dialog("close"); } }, Cancel: function () { $(this).dialog("close"); } }, close: function () { allFields.val("").removeClass("ui-state-error"); } }); } }); $(".make").click(function () { if ($("input[id$='[" + currentID + "].r_make'").val().length max || o.val().length < min) { o.addClass("ui-state-error"); updateTips("Length of " + n + " must be between " + min + " and " + max + "."); return false; } else { return true; } } function checkRegexp(o, regexp, n) { if (!(regexp.test(o.val()))) { o.addClass("ui-state-error"); updateTips(n); return false; } else { return true; } } $("#make-form").dialog({ autoOpen: false, resizable: false, bgiframe: true, height: 190, width: 350, modal: true, buttons: { "Add Make": function () { var bValid = true; allFields.removeClass("ui-state-error"); bValid = bValid && checkLength(make, "Make", 1, 2000); if (bValid) { $("input[id$='[" + currentID + "].r_make'").val(make.val()); $(this).dialog("close"); } }, Cancel: function () { $(this).dialog("close"); } }, close: function () { allFields.val("").removeClass("ui-state-error"); } }); } }); $(function () { $("tr td .delete").live("click", function () { if ( !! confirm("Delete?")) { $(this).parent().parent().remove(); return false; } else { ($(this).close()) } }); }) } } }); }); $(function () { $("input[id$='[0].r_model'").click(function () { if ($("input[id$='[0].r_model'").val().length max || o.val().length < min) { o.addClass("ui-state-error"); updateTips("Length of " + n + " must be between " + min + " and " + max + "."); return false; } else { return true; } } function checkRegexp(o, regexp, n) { if (!(regexp.test(o.val()))) { o.addClass("ui-state-error"); updateTips(n); return false; } else { return true; } } $("#model-form").dialog({ autoOpen: false, resizable: true, bgiframe: true, height: 290, width: 450, modal: true, buttons: { "Add Model": function () { var bValid = true; allFields.removeClass("ui-state-error"); bValid = bValid && checkLength(model, "Customer Notes", 1, 2000); if (bValid) { $("input[id$='[0].r_model'").val(model.val()); $(this).dialog("close"); } }, Cancel: function () { $(this).dialog("close"); } }, close: function () { allFields.val("").removeClass("ui-state-error"); } }); } }); }); $(function () { $("input[id$='[0].r_make'").click(function () { if ($("input[id$='[0].r_make'").val().length max || o.val().length < min) { o.addClass("ui-state-error"); updateTips("Length of " + n + " must be between " + min + " and " + max + "."); return false; } else { return true; } } function checkRegexp(o, regexp, n) { if (!(regexp.test(o.val()))) { o.addClass("ui-state-error"); updateTips(n); return false; } else { return true; } } $("#make-form").dialog({ autoOpen: false, resizable: false, bgiframe: true, height: 190, width: 350, modal: true, buttons: { "Add Make": function () { var bValid = true; allFields.removeClass("ui-state-error"); bValid = bValid && checkLength(make, "Make", 1, 2000); if (bValid) { $("input[id$='[0].r_make'").val(make.val()); $(this).dialog("close"); } }, Cancel: function () { $(this).dialog("close"); } }, close: function () { allFields.val("").removeClass("ui-state-error"); } }); } }); });

这是完整的代码。

我已经尝试了标准的.valid()但它提出了css但仍然处理按钮单击,还有一个空字段但我正在努力使这个工作与唯一生成的id’s努力

如果我清楚地理解你的意思(由于缺乏信息,我可以说它有点困难;-)),作为AJAX调用创建一个新的文本框,生成名称/ id。 此文本框充当用户的容器,可以输入您需要再次validation预定义规则集的内容

您必须记住,在表单上应用validate方法时,您的新手复选框不是DOM的一部分。 然后validation插件是未知的

请检查validation插件的动态表单演示并研究代码,尤其是以delegate开头的部分

我没有看到任何代码,我没有正确理解你的问题,但可能是你的validation代码是在发送ajax请求之前加载的页面中的document.ready事件中吗?

尝试将validation代码放入函数中,并在ajax请求成功时运行该函数。