如何在不使用runat = server的情况下访问服务器端c#上的html表

时间:2015-04-03 06:37:24

标签: c# jquery c#-4.0

我在html表上创建动态行并尝试在c#上获取表值。但每当我把runat=server放在表上jQuery不起作用,因此我无法访问服务器端的html表。请给我一些建议。

<table id="purchaseItems" name="purchaseItems" align="center">
    <tr>
        <th>Paragraph</th>
        <td>
            <input type="text" name="description[]" class="tbDescription next" required />
        </td>                               
        <td>
            <input type="button" name="addRow[]" class="add" value='+' />
        </td>
        <td>
            <input type="button" name="addRow[]" class="removeRow" value='-' />
        </td>
    </tr>
</table>

这是我的jQuery:

$(document).ready(function () {

$(document).on('click', '#purchaseItems .add', function () {

    var row = $(this).closest('tr');
    var clone = row.clone();

    // clear the values
    var tr = clone.closest('tr');
    tr.find('input[type=text]').val('');

    $(this).closest('tr').after(clone);

});

$(document).on('keypress', '#purchaseItems .next', function (e) {
    if (e.which == 13) {
        var v = $(this).index('input:text');
        var n = v + 1;
        $('input:text').eq(n).focus();
        //$(this).next().focus();
    }
});

$(document).on('keypress', '#purchaseItems .nextRow', function (e) {
    if (e.which == 13) {
        $(this).closest('tr').find('.add').trigger('click');
        $(this).closest('tr').next().find('input:first').focus();
    }
});
$(document).on('click', '#purchaseItems .removeRow', function () {
    if ($('#purchaseItems .add').length > 1) {
        $(this).closest('tr').remove();
    }
});

});

1 个答案:

答案 0 :(得分:1)

您的问题出在ID中,由asp.net生成。添加runat =“server”后,它不会只是purchaseItems。要获得真实ID,您应该使用下一个代码:

$(document).on('keypress', '#<%= purchaseItems.ClientID %> .next', function (e) {
    if (e.which == 13) {
        var v = $(this).index('input:text');
        var n = v + 1;
        $('input:text').eq(n).focus();
        //$(this).next().focus();
    }
});

但在这种情况下,您将无法分隔js和asp.net文件。作为替代方法,您可以从任何浏览器使用开发工具,以确定将在客户端生成哪个ID。