使用jquery和c#从span检索数据
本文关键字:span 检索 数据 jquery 使用 | 更新日期: 2023-09-27 17:58:19
我试图从td内的span中检索一些值。我有这个函数:
function reorder() {
alert("Entrando en reorder");
strorder = "";
var totalid = $('#ctl00_ContentPlaceHolder1_grdResultados tr td input').length;
alert(totalid);
for (var i = 0; i < totalid; i++) {
strorder = strorder +$('#ctl00_ContentPlaceHolder1_grdResultados tr td span')[i].text()+"*"+ $('#ctl00_ContentPlaceHolder1_grdResultados tr td input')[i].getAttribute("value") + "|";
alert("strorder1");
}
//strorder = window.location.href;
alert("strorder2");
}
源代码是这样的(一段代码):
<table class="table table-hover tablaDimensiones" cellspacing="0" border="0" id="ctl00_ContentPlaceHolder1_grdResultados" style="border-collapse:collapse;">
<tr>
<th scope="col">
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdResultados','Sort$titulo')">Título</a>
</th>
<th scope="col">Opciones</th>
</tr>
<tr>
<td>
<span id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_lblTitulo">Dimension1</span>
<input type="hidden" name="ctl00$ContentPlaceHolder1$grdResultados$ctl02$hdnid" id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_hdnid" value="1" />
</td>
<td>
<a id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_cmdEditar" title="Editar" class="btn btn-default" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdResultados$ctl02$cmdEditar','')">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</a>
<a onclick="return confirm('¿Desea elimnar la dimensión?');" id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_cmdEliminar" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdResultados$ctl02$cmdEliminar','')">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</a>
</td>
</tr>
<tr>
我该怎么做?,因为这样我就没有我想要的了。问候
我相信下面的例子就是你想要的——你几乎已经得到了正确的代码——你应该稍微组织一下。
注意:代码中唯一的错误在长行中:
strorder = strorder +$('#ctl00_ContentPlaceHolder1_grdResultados tr td span')[i].text()+"*"+ $('#ctl00_ContentPlaceHolder1_grdResultados tr td input')[i].getAttribute("value") + "|";
希望这能有所帮助。
function reorder() {
var strorder = "";
var table_spans = $('#ctl00_ContentPlaceHolder1_grdResultados span[id^="ctl00_ContentPlaceHolder1"]');
var table_inputs = $('#ctl00_ContentPlaceHolder1_grdResultados input');
for (var i = 0; i < table_inputs.length; i++)
{
var span_text = table_spans.eq(i).text();
var input_value = table_inputs.eq(i).val();
strorder += span_text + "*" + input_value + "|";
}
console.log(strorder);
}
reorder();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<table class="table table-hover tablaDimensiones" cellspacing="0" border="0" id="ctl00_ContentPlaceHolder1_grdResultados" style="border-collapse:collapse;">
<tr>
<th scope="col">
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdResultados','Sort$titulo')">Título</a>
</th>
<th scope="col">Opciones</th>
</tr>
<tr>
<td>
<span id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_lblTitulo">Dimension1</span>
<input type="hidden" name="ctl00$ContentPlaceHolder1$grdResultados$ctl02$hdnid" id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_hdnid" value="1" />
</td>
<td>
<a id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_cmdEditar" title="Editar" class="btn btn-default" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdResultados$ctl02$cmdEditar','')">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</a>
<a onclick="return confirm('¿Desea elimnar la dimensión?');" id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_cmdEliminar" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$grdResultados$ctl02$cmdEliminar','')">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</a>
</td>
</tr>
</table>
这可能是乐观的
$td = $('.tablaDimensiones tr td');
var spans = $td.find('span');
var inps = $td.find('input');
var len = spans.length;
var sortorder = "";
for (var i = 0; i < len; i++) {
sortorder += spans.eq(i).text() + "*" + inps.eq(i).text() + "|";
}
此代码获取表td.中每个跨度内的所有文本
var strorder = "";
var spans=$("table.table").find("td").find("span");
var inputs=$("table.table").find("td").find("input");
for (var i=0; i<spans.length; i++){
strorder+=$(spans[i]).text()+"*"+$(inputs[i]).val()+"|";
}
或者没有第二个阵列:
var strorder = "";
var spans=$("table.table").find("td").find("span");
for (var i=0; i<spans.length; i++){
var $span=$(spans[i]); //temporary jquery object
strorder+=$span.text()+"*"+$span.next().val()+"|"; //$span.next() ==input
}
PS不需要将array.length
保存到totalid,因为长度是属性而不是方法,所以在循环中获取长度是最佳解决方案。