我有一个动态创建的文本框集合(通过按钮单击),并且所有文本框都是必需的。一旦文本框为空,就应该在当前验证摘要中添加一条错误消息。有关如何执行此操作的任何帮助。
Javascript代码
var rowsid = 0;
// Add button functionality
$("#btnAddButton").live("click", function () {
rowsid++;
var master = $(this).parents().find("table.dynamictable"); //$(this).parents("table.dynatable");
// Get a new row based on the prototype row
var prot = master.find(".prototype").clone();
prot.attr("class", "");
prot.find(".id").attr("value", "00000" + rowsid);
master.find("tbody").append(prot);
});
HTML代码
<div class="msg-body">
<table class="dynamictable">
<thead>
<tr>
<th style="display: none"></th>
<th>Package Name</th>
<th>Package Cost</th>
<th>No. of Attendees</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr class="prototype">
<td style="display: none"><input type="hidden" name="id[]" value="" class="id" /></td>
<td><input type="text" name="packagename[]" value="" class="pkname" data-val-required="This field is required" /></td>
<td><input type="text" name="packagecost[]" value="0" class="pkcost" style="text-align: right" /></td>
<td><input type="text" name="noofattendees[]" value="0" class="nfattendees" style="text-align: right"/></td>
<td>@Html.ActionImage("", null, "~/Content/Images/delete.gif", "Delete", new { @class = "deleterows" }) </td>
</tr>
</tbody>
</table>
<div style="padding:20px 0 20px 100px "><input type="button" id="btnAddButton" value="Add New" class="button addbutton"/></div>
</div>
答案 0 :(得分:1)
function refreshValidation() {
$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("form");
}
这将验证您动态添加的文本框
答案 1 :(得分:0)
使用HTML5 required="required"
属性,但它不适用于旧版本的IE
<input type="text" name="usrname" required="required" />
详细说明 - http://www.w3schools.com/html5/att_input_required.asp