我正在尝试从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(&#39;ctl00$ContentPlaceHolder1$grdResultados&#39;,&#39;Sort$titulo&#39;)">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(&#39;ctl00$ContentPlaceHolder1$grdResultados$ctl02$cmdEditar&#39;,&#39;&#39;)">
        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
      </a>
      &nbsp;
      <a onclick="return confirm(&#39;¿Desea elimnar la dimensión?&#39;);" id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_cmdEliminar" href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolder1$grdResultados$ctl02$cmdEliminar&#39;,&#39;&#39;)">
        <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(&#39;ctl00$ContentPlaceHolder1$grdResultados&#39;,&#39;Sort$titulo&#39;)">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(&#39;ctl00$ContentPlaceHolder1$grdResultados$ctl02$cmdEditar&#39;,&#39;&#39;)">
        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
      </a>
      &nbsp;
      <a onclick="return confirm(&#39;¿Desea elimnar la dimensión?&#39;);" id="ctl00_ContentPlaceHolder1_grdResultados_ctl02_cmdEliminar" href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolder1$grdResultados$ctl02$cmdEliminar&#39;,&#39;&#39;)">
        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
      </a>

    </td>
  </tr>
</table>

09-16 18:26