【发布时间】: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" /> '; echo
'<button id="button" ">SELECT</button> '; echo '<input type="hidden"
name="selectedRows" id="selectedRows" value="1" /> '; echo '</form>';
我对 Javascript/Jquery 很陌生,所以我边走边学。
任何有关这方面的帮助或建议将不胜感激。
【问题讨论】:
-
选择行的点击事件在哪里?
-
@madalinivascu 它位于单独的脚本标签中。 echo "";
-
如何将这些数据提交到数据库?
-
你的提交事件在哪里?
-
现在我只需要将选定的行放入数组中。一旦它工作,我可以将数据获取到数据库。我目前的问题是我只能将所有行放入数组中,而不仅仅是选定的行。
标签: javascript php jquery html arrays