通过javascript增加id属性值

时间:2016-03-30 20:43:03

标签: javascript asp.net

我正在尝试动态地向表中添加行以接受订单,并为其创建了function addnewrow() { var lastid = $("#table tr:last").attr("id"); var newid=lastid+1; var newcolumn = document.createElement("tr"); newcolumn.id=newid; newcolumn.innerHTML = "<td id='no"+newid+"'><a class='cut'>-</a>"+newid+"</td>"+ "<td>"+ "<ajaxToolkit:ComboBox ID='prod"+newid+"' runat='server' DataSourceID='SqlDataSource2' DataTextField='pname' DataValueField='pid' MaxLength='0' style='display: inline;'></ajaxToolkit:ComboBox>" + "</td>"+ "<td><input type='number' required='required' min='1' name='quantity" + newid + "' /></td>" + "<td id='price" + newid + "'></td>" + "<td id='amount" + newid + "'></td>"; document.getElementById("table").appendChild(newcolumn); } 函数。

protected global::AjaxControlToolkit.ComboBox prod" + newid + ";

我这样做是为了获取代码隐藏文件中所有元素的值,将它们放入数据库中。 但由于这个原因,我在aspx.designer.cs页面中收到一条错误,表示分号是预期的

<table class="Grid" id="table">
        <tr>
            <td colspan="5">Enter Order Details</td>
        </tr>
        <tr>
            <th>Sr No.</th>
            <th>Product</th>
            <th>Quantity</th>
            <th>Price</th>
            <th>Amount</th>
        </tr>
        <tr id="1">
            <td><a class="cut">-</a>1</td>
            <td>
                <ajaxToolkit:ComboBox ID="prod1" runat="server" DataSourceID="SqlDataSource2" DataTextField="pname" DataValueField="pid" MaxLength="0" style="display: inline;"></ajaxToolkit:ComboBox>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:micoConnectionString %>" SelectCommand="SELECT [pid], [pname] FROM [Products]"></asp:SqlDataSource>
            </td>
            <td><input type="number" required="required" min="1" name="quantity1" /></td>
            <td id="price1"></td>
            <td id="amount1"></td>
        </tr>
    </table>
<a class="add" onclick="addnewrow()" href="#">+</a>

ASP.NET代码

var str = "111222 111 111 22111";
var reg = /1+/g;

console.log(str.match(reg));

1 个答案:

答案 0 :(得分:0)

检查

$("#btnAddSchedule").click(function () {
    var trs = $("[id^=trSchedules]");
    var numberofrows = trs.length;
    var newtr = $('#' + trs[0].id).clone();
    $(newtr).attr('id', $(newtr).attr('id').replace(/\d+/, numberofrows));
    newtr.find("input,select,img").each(function () {
        $(this).attr('id', $(this).attr('id').replace(/\d+/, numberofrows));
        $(this).attr('name', $(this).attr('name').replace(/\d+/, numberofrows));
        if ($(this).attr('type') != "hidden") {
            $(this).val('');
        }
        else if ($(this).attr('id').indexOf('DataExportQueueID') == -1) {
            $(this).val('');
        }
        if ($(this).attr("type") == "checkbox") {
            $(this).removeAttr("checked");
            $(this).parent().html($(this).parent().html().replace(/\d+/g, numberofrows));
        }
        if ($(this).attr("type") == "button") {
            $(this).attr("onclick", "deleteSchedule(this,0);");
        }
    });
    $('#' + trs[numberofrows - 1].id).after(newtr);
    CrossCheckScheduleRows();
});
function CrossCheckScheduleRows() {

    $('[id^=trSchedules]').each(function () {
        var row = $(this);
        var index = row[0].rowIndex - 2;
        row.attr('id', row.attr('id').replace(/\d+/, index));
        row.find("input,select,img").each(function () {
            $(this).attr('id', $(this).attr('id').replace(/\d+/, index)).attr('name', $(this).attr('name').replace(/\d+/, index));

        });
    });

}

我将此用于同一目的,也许它会帮助你