【问题标题】:Load selected Rows from html table to Array将选定的行从 html 表加载到数组
【发布时间】:2017-10-23 07:50:24
【问题描述】:

我的任务的一些上下文 - 用户看到一个表并选择要通过 sql 上传到主 sql 表的行。

我的问题 - 我需要能够仅将表中选定的行加载到数组中。

现在我只能将整个表加载到 Javascript 中的数组中,然后将其发布到 php 页面,然后 print_r 数组,我可以通过这段代码获取所有值:

echo '<script>
    var myTableArray = [];
    $("#uploads_approval tr").each(function() {
        var arrayOfThisRow = [];
        var tableData = $(this).find("td");
            if (tableData.length > 0) {
                    tableData.each(function() { 
                        arrayOfThisRow.push($(this).text()); 
                    });
                        myTableArray.push(arrayOfThisRow);
                }

        });

        var selectedRows = document.getElementById("selectedRows");
        selectedRows.value = myTableArray;
</script>';

我已经尝试了多种方法来仅获取选定的行,例如从以下位置更改此行:

  $("#uploads_approval tr").each(function() {

到这里:

  $("#uploads_approval tr.selected").each(function() {    

但是我的数组输出什么也没返回。

我也尝试过类似的方法,但也没有成功

echo "<script>
    var Array2 = [];
    $('#uploads_approval tr').each(function() {
    var tableData = $(this).find('td');
    for(i = 0; i < tableData.length; i++){
        tableData.each(function() { 
                    var thisRow = 
document.getElementByClassName('selected').Text();
                    array2.push(thisRow); 
                });

    }
    var selectedRows = document.getElementById('selectedRows');
    selectedRows.value = Array2;
}
</script>";

选择行的点击事件:

echo "<script> $('#uploads_approval tr').click(function(){ $(this).toggleClass('selected'); }); </script>"; 

提交活动表单:

echo '<form action="uploads_approval.php" method="post" >' . '<input 
type="hidden" name="uploadRows" id="uploadRows" value="1" /> &nbsp;'; echo 
'<button id="button" ">SELECT</button>&nbsp;'; echo '<input type="hidden" 
name="selectedRows" id="selectedRows" value="1" /> &nbsp;'; echo '</form>';

我对 Javascript/Jquery 很陌生,所以我边走边学。

任何有关这方面的帮助或建议将不胜感激。

【问题讨论】:

  • 选择行的点击事件在哪里?
  • @madalinivascu 它位于单独的脚本标签中。 echo "";
  • 如何将这些数据提交到数据库?
  • 你的提交事件在哪里?
  • 现在我只需要将选定的行放入数组中。一旦它工作,我可以将数据获取到数据库。我目前的问题是我只能将所有行放入数组中,而不仅仅是选定的行。

标签: javascript php jquery html arrays


【解决方案1】:

每次单击 tr 时更新您的值

$('#uploads_approval tr').click(function(){

 $(this).toggleClass('selected');
 var row = [];
 $('.selected').each(function(i,v){
     row.push($(v).text());
  });
  $('#selectedRows').val(row);
 });

$('#uploads_approval tr').click(function() {

  $(this).toggleClass('selected');
  var row = [];
  $('.selected').each(function(i, v) {
    row.push($(v).text());
  })

  $('#selectedRows').val(row);
});
.selected {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<br>
<table id="uploads_approval">
  <tbody>
    <tr>
      <td>tr1</td>
    </tr>
    <tr>
      <td>tr2</td>
    </tr>
    <tr>
      <td>tr3</td>
    </tr>
    <tr>
      <td>tr4</td>
    </tr>

  </tbody>
</table>
<input type="text" id="selectedRows">

【讨论】:

  • 谢谢,现在看。你介意解释一下这是做什么的吗? $('#selectedRows').val(row);
  • 它的 jquery 等价于var selectedRows = document.getElementById('selectedRows'); selectedRows.value = Array2;
  • 似乎不起作用。添加那段代码后,不再选择行。 $('#uploads_approval tr').click(function(){ $(this).toggleClass('selected'); var row = []; $('.selected').each(function(i,v){行[] = $(v).val(); }); $('#selectedRows').val(row); });
  • 不,同样的事情。函数中的 i,v 是做什么用的? $('.selected').each(function(i,v){ 对不起,我问了很多问题,因为我试图理解它而不是仅仅复制它。
  • i 是索引,v 是值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-17
  • 2017-08-08
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多