【问题标题】:Test For Empty Text Inputs In A Table Row测试表格行中的空文本输入
【发布时间】:2018-03-19 20:50:01
【问题描述】:

我有一个引导模式,如果有要显示的数据行,我会用数据库中的数据填充它。 (名字、姓氏、班级排名)。

我可以在保存数据之前添加和删除行,并且 我希望能够从表中收集数据行并创建一个可以在保存时传递到后端的数据对象。我目前的代码如下:

HTML

<?php foreach ($athletes as $i => $row) { ?>
        <?php $i == 0 ? $rownum = 1 : $rownum = $i+1; ?>
        <tr id="<?php echo $i; ?>">
          <td><?php echo $rownum; ?><input type="hidden" name="data[id]" value="<?php echo $row['id']; ?>" ></td>
          <td><input type='text' name="data[first_name]" class="form-control" value="<?php echo $row['first_name']; ?>" ></td>
          <td><input type='text' name="data[last_name]" class="form-control" value="<?php echo $row['last_name']; ?>" ></td>
          <td><input type='text' name="data[classrank]" class="form-control" value="<?php echo $row['class']; ?>" ></td>
          <td><button type="button" id="remScnt" class="btn btn-default">Remove Row</button></td>
        </tr>
    <?php } } ?>

jQuery 保存函数

$("button#saveaths").click(function() {
  var user_id = $("#user_id").val();
  var ath_data = [];
  $("tr").each(function() {

      if (data[first_name] != '') {
        ath_data.push(this.data[first_name]);
        ath_data.push(this.data[last_name]);
        ath_data.push(this.data[classrank]);
        ath_data.push(user_id);
      }
  });
});

我在 if 语句中遇到错误。

在这个阶段,我只是尝试构建可以传递给 codeigniter 控制器以插入到我的数据库中的数据对象。

此外,如果有更好的方法可以做到这一点,我愿意接受建议。

提前致谢!

【问题讨论】:

  • 如果你的错误是在 jQuery if 语句上,那么用于生成的 PHP 是无关紧要的。重要的是从 PHP 生成的 HTML 的样子。另外,一个建议是,当你在这个问题中做内联 PHP 时,你可以使用 endforeach 之类的东西 - stackoverflow.com/questions/4600419/endforeach-in-loops

标签: php jquery codeigniter


【解决方案1】:

您没有使用正确的语法来获取字段的值。

试试这个

$("button#saveaths").on("click",function() {
  var userID = $("#user_id").val();
  var ath_data = [];
  $("tr").each(function() {
    var firstName = $(this).find("[name=data[first_name]]").val();
    if (firstName != '') {
      var lastName = $(this).find("[name=data[last_name]]").val(),
          classRank = $(this).find("[name=data[classrank]]").val();
      ath_data.push({firstName:firstName,lastName:lastName,classRank:classRank,userID:userID});
    }
  });
  if (ath_data.length>0) {
    $.ajax({ 
      type="POST",
      url:"someServerProcess.php",
      data: JSON.stringify(ath_data),
      contentType: "application/json"
    });
  }
});

【讨论】:

  • 我收到一个错误:未捕获的错误:语法错误,无法识别的表达式:name=data[first_name] at Function.ga.error (jquery-3.2.1.js:2) 这是由于到 jquery 的版本?
  • 我的错,我错过了围绕着你讨厌的名字的 [] - 我刚开始时就有它们,然后出于某种原因我删除了它们。请在minimal reproducible example 中仅使用 HTML 和 JS 更新您的问题
  • 我使用命名方案 data[first_name] 等的原因是为了区分每一行。我将修改您的代码和命名方案,看看问题是否仍然存在。
  • 我最终删除了括号并且代码正在运行!感谢您对此的帮助!
  • 发现另一个问题:As Is ath_data.push({firstName:name,lastName:lastName,classRank:classRank,userID:userID});成为 ath_data.push({firstName:firstName,lastName:lastName,classRank:classRank,userID:userID});
猜你喜欢
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2016-04-06
  • 1970-01-01
相关资源
最近更新 更多