0

我有一个动态创建的文本框集合(通过按钮单击),并且所有文本框都是必需的。一旦文本框为空,则应将错误消息添加到当前验证摘要中。有关如何执行此操作的任何帮助。

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>
4

2 回答 2

1
function refreshValidation() {
    $("form").removeData("validator");
    $("form").removeData("unobtrusiveValidation");
    $.validator.unobtrusive.parse("form");
}

这将重新验证您动态添加的 texbox

于 2012-06-14T09:45:12.773 回答
0

使用 HTML5required="required"属性,但在旧版本的 IE 中不起作用

<input type="text" name="usrname" required="required" />

详解-http: //www.w3schools.com/html5/att_input_required.asp

于 2012-06-14T09:10:20.373 回答