迭代通过表寻找选择

时间:2015-10-22 20:08:12

标签: javascript jquery

我有一个表,我通过ajax函数动态填充。

            <!-- Tab4 -->
        <div id="ServiceOrderTasks" class="tab-pane">
          <div class="widget white-full">
            <div class="widget-body">
              <div class="row-fluid">
                <!-- BEGIN COLUMN -->
                <div class="span12">
                  <!--BEGIN LABEL & BADGE PORTLET-->
                  <table id="tasktable" class="table table-bordered table-striped">
                    <thead>
                      <tr>
                        <th colspan="2">Service Order Task</th>
                        <th>Status</th>
                      </tr>
                    </thead>
                    <tbody id="taskTableContents">

                    </tbody>
                  </table>
                  <!--END LABEL & BADGE PORTLET-->
                </div>
                <!-- END COLUMN -->
              </div>
            </div>
          </div>
        </div>

ajax代码写入内容

function GetServiceOrderDetailsOnComplete4(result) {
var content = "";

  $('#taskTableContents > tbody').empty(); 

  var records = result.split("*");
  var len = records.length - 1; 
  var id_name = "";
  var field = "";

  for (var i = 0; i < len; ++i) {
    var record = records[i].split("|");
    id_name = "";
    id_name = "taskID|" + record[0] + "|" + record[1];

    field = record[38].trim();
    if (field == "1") {
      content = content + "<tr><td colspan='2'>" + record[2] + "</td>" +
      "<td>" +
      "  <select name='" + id_name + "' id='" + id_name + "' data-role='slider' data-mini='true'>";

  switch (record[14]) {
  case "0":
    content = content + "    <option value=1>Completed</option>" +
      "    <option value=0 selected>Incomplete</option>" +
      "    <option value=0>Not Applicable</option>";
    break;

  case "1":
    content = content + "    <option value=1 selected>Completed</option>" +
      "    <option value=0>Incomplete</option>" +
      "    <option value=0>Not Applicable</option>";
    break;
  case "2":
    content = content + "    <option value=1>Completed</option>" +
      "    <option value=0>Incomplete</option>" +
      "    <option value=0 selected>Not Applicable</option>";
    break;

  default:
    content = content + "    <option value=1>Completed</option>" +
      "    <option value=0 selected>Incomplete</option>" +
      "    <option value=0>Not Applicable</option>";

    break;

  }


  content = content + "  </select>" +
    "</td>" +
    "</tr>";
    };
  };
  $("#taskTableContents").append(content);
}

function GetServiceOrderDetailsOnFail(result) {
}

虽然非常混乱,但上面的代码可以使用。

这是一个问题:我需要遍历表并获取每一行。

有一些条件代码根据所选值设置标志。

  //  $('#tasktable > tbody  > tr > td > select').each(function () {
  $('#taskTableContents > tbody  > tr > td > select').each(function () {
    var parms = this.id.split("|");
    var sor = parms[1];
    var seq = parms[2];
    var v = $(this).val();   // will get the value correctly
    switch (v) {
      case "0":
//        UpdateServiceOrderDetailStatus(sor, seq, 0);
      okToClose = false;
      break;
      case "1":
        UpdateServiceOrderDetailStatus(sor, seq, 1);
      break;
      case "2":
//        UpdateServiceOrderDetailStatus(sor, seq, 0);
      okToClose = false;
      break;
    }
  });

在执行代码期间, 调试器到达此行并直接跳转到okToClose = false行。

  $('#taskTableContents > tbody  > tr > td > select').each(function () {

我错过了什么?

1 个答案:

答案 0 :(得分:0)

而不是尝试使用..

解析动态表
#taskTableContents > tbody  > tr > td > select

参考一个非常具体的元素,我会在你的选择器上添加一个类,然后迭代文档中的每个选择器,直到你找到你想要的那个。这样你甚至不用担心表格布局。

<select class="select"/>
   <options>
</select>
$('.select').each(function() {
   //We have each select now as this
   if(this.id === somethingIWant) {
        //do something with this select
   };
});

希望这能为你清除它,如果没有,也许你可以提供你想要实际做的其他描述,或者使你的代码的jsfiddle无法正常工作。