Javascript 访问动态表的元素
大家好,我有以下HTML代码,它生成带有字段的行,每行有几个字段,对于行1,有一个名为“sku1”的输入,等等: 问题是,当页面加载时,没有任何输入skuX存在,因此无法从后面的代码进行访问 表HTML中的代码Javascript 访问动态表的元素,javascript,asp.net,vb.net,code-behind,Javascript,Asp.net,Vb.net,Code Behind,大家好,我有以下HTML代码,它生成带有字段的行,每行有几个字段,对于行1,有一个名为“sku1”的输入,等等: 问题是,当页面加载时,没有任何输入skuX存在,因此无法从后面的代码进行访问 表HTML中的代码 <table class="table table-striped" style="margin:10px" runat="server" id="tablaSKU" data-row-prototype=""<tr> <
<table class="table table-striped" style="margin:10px" runat="server" id="tablaSKU" data-row-prototype=""<tr> <th>__id__</th> <th class="col-sm-2" name="sku"> <input type="text" id="sku__id__" class="form-control" runat="server"> </th> <th class="col-sm-1"> <input type="text" id="precio__id__" class="form-control" runat="server"> </th> <th class="col-sm-3"> <input type="text" id="descripProd__id__" class="form-control" runat="server"> </th> <th class="col-sm-2"> <input type="date" id="dataInit__id__" class="form-control" runat="server"> </th> <th class="col-sm-2"> <input type="date" id="dataFin__id__" class="form-control"runat="server"> </th> <th class="col-sm-2"> <select id="opcion__id__" runat="server" class="formularioChekLst form-control"> </option> <option>Opción">
<thead>
<tr>
<th></th>
<th>SKU</th>
<th>Precio €</th>
<th>Descripción</th>
<th>Fecha Inicio Demo</th>
<th>Fecha Fin Demo</th>
<th>Tipo de préstamo</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
您的代码流是什么?您试图实现什么?您还需要在逻辑后面添加代码。是什么导致您回发到服务器?老实说,如果可以的话,我建议您转向MVC框架。有很多免费视频,而且它的前端开发得很好,就像你正在使用的一样。@Patel我有一个脚本可以自动创建行,每行都有一个input1,input2类型的输入。。。。我想访问input1,input2,但后面的代码无法识别这些元素inputX,因为它们并不总是存在。我想知道的是,如果有什么事情要做,比如如果inputX存在,那么取它的值,否则忽略它。例如,如果我想访问第5列的输入,我会执行类似input5.Text的操作,但后面的代码不会将其识别为未明确声明的内容,如果javascript执行,它会在执行时显示。
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
var table = $('#tablaSKU');
rowCount = 1;
function appendRow() {
var content = table.data('row-prototype').replace(/__id__/g, rowCount);
table.find('tbody').append(content);
++rowCount;
document.getElementById('rowCounter').value = rowCount - 1;
}
$(function () {
appendRow(); // Append the first row on page load
$('div#myform').on('change', function () {
var inputs = $(this).find(':input,select'),
valid = true;
$.each(inputs, function () {
if ($(this).val() == '') {
valid = false;
}
});
if ((valid) && rowCount < 11) {
appendRow();
}
if ((valid) && rowCount >= 11) {
window.alert("Ha alcanzado el número máximo de SKUs(10)");
}
});
});
</script>