循环问题? jQuery的

时间:2015-06-12 15:52:45

标签: jquery html asp-classic

我有一个动态表,它有三个文本框,每行添加一个选择下拉列表。单击按钮,可以动态添加更多行。一旦我点击提交,我想循环通过每个单元格,但只在tbody部分;尝试循环时只是跳过了。我已经尝试在控制台中进行调试,但没有发现任何问题。

这是我的HTML:

<table id= "tb1" class="norm sideBG" name="recordTable" align="center" cellpadding="4" cellspacing="0" border="1" bordercolor="lightgrey" width="600px">
  <tr class="norm">
    <td>
      <b><u>LMV PART #</u></b>
    </td>
    <td>
      <b><u>LOCATION</u></b>
    </td>
    <td>
      <b><u>QUANTITY</u></b>
    </td>
    <td>
      <b><u>WHERE USED</u></b>
    </td>
  </tr>
  <tbody class="theRow" id="theRow">
    <tr>
      <td class="norm">
        <label id="partLabel" style="color : red;"></label>
        <br />
        <input type='text' name='txtbox0[]' id='txtbox0[]' onchange="javascript: changeLabel(this)" />
      </td>
      <td class="norm">
        <select id="txtbox1[]" name="txtbox1[]" >
          <%
             strSQL1 = "myQuery"
             Set oRs1 = oConn1.Execute(strSQL1)
             Do while not oRs1.eof
             Response.Write("<OPTION VALUE='" & Mid(oRs1(0),4) & "'")
             Response.Write(">" & Mid(oRs1(0),4) & "</OPTION>")
             oRs1.movenext
             loop
             oRs1.Close
             oConn1.Close
             %>
            </select>
          </td>
      <td class="norm">
        <input type='text' name='txtbox2[]' id='txtbox2[]' />
      </td>
      <td class="norm">
        <input type='text' name='txtbox3[]' id='txtbox3[]' />
      </td>
    </tr>
  </tbody>
</table>

与我的jQuery循环一起在submit()

上处理
var cellVal;
$("#tb1 > tbody.theRow > tr").each(function() {
  cellVal = $(this).text();
  if (cellVal.length == 0 || cellVal == null)
  {
    alert("there's a blank field");
    blankFields = true;
  }
});

1 个答案:

答案 0 :(得分:1)

each上使用td代替tr

var cellVal;
$("#theRow tr td").each(function () {

    // If td contain textbox
    if ($(this).find('input').length) {
        cellVal = $.trim($(this).val());
        // Get value of textbox
    } else if ($(this).find('select').length) {
        // If contains drop-down get the selected option value
        cellVal = $(this).find('select option:selected').val();
    }

    if (!cellVal.length) {
        alert("there's a blank field");
        blankFields = true;
    }
});

此外,您在文本框和下拉菜单的名称中不需要[]