[英]Looping through HTML table and store td values as a string using jquery / javascript
I have following table 我有下表
while($row=mysql_fetch_array($sql))
{
$id=$row['product_id'];
$name=$row['name'];
$stock=$row['stock'];
?>
<tbody>
<tr >
<td class="edit_td">
<span id="first_<?php echo $id; ?>" class="text"><?php echo $name; ?></span>
</td>
<td class="edit_td">
<span id="last_<?php echo $id; ?>" class="text"><?php if($stock==0){echo 0;}else{echo $stock;} ?></span>
</td>
<td class="edit_td1" id="<?php echo $id; ?>">
<div class="form-group">
<div class="col-lg-3 inputContainer">
<input class="form-control cls-quantity" id="quanti_<?php echo $id; ?>" name="number" type="text" />
</div>
</div>
</td>
<td class="action_td" id="<?php echo $id; ?>"><span class="glyphicon glyphicon-remove" style="cursor:pointer;color:red;"></span></td>
</tr>
</tbody>
<?php
}
?>
And button: 和按钮:
<a href="customer_details.php?shop=<?php echo $_GET['shop'];?>" class="btn btn-info" role="button" onclick="return td_validation();store_value_as_string();">Generate Bill</a>
Question: Here I want to return a function store_value_as_string();
问题:这里我想返回一个函数
store_value_as_string();
after td_validation();
在
td_validation();
which loop through all <tr>
and get id
of class="edit_td1"
and associated value of id="quanti_<?php echo $id; ?>"
. 循环遍历所有
<tr>
并获取class="edit_td1"
id
和id
class="edit_td1"
id="quanti_<?php echo $id; ?>"
相关值。 Then function store_value_as_string();
然后是函数
store_value_as_string();
will gives me two strings 会给我两个字符串
str1 = 121,122,123,124;//id of class="edit_td1"
str2 = 2,3,1,4;//associated value of id="quanti_<?php echo $id; ?>"
these two strings are required for ajax call(pass to other php page). 这两个字符串是ajax调用所必需的(传递给其他php页面)。 I actually have code for doing same but it runs
onchange
of ".edit_td1"
but the sequence of operations by -->tester<-- ie onchange-blur(leave the textbox for same <tr> -onchange-blure-onchange...
gives me wrong output. 其实,我的代码做相同的,但它运行
onchange
的".edit_td1"
,但操作的顺序- >测试< -即onchange-blur(leave the textbox for same <tr> -onchange-blure-onchange...
让我错输出。
You might wanna use the Jquery each . 您可能希望每个都使用Jquery 。 Iterate over all td's with the class '.edit_td1'.
使用类'.edit_td1'迭代所有td。 Fetch their id's using .attr('id') and add to an array as well and then later use Jquery's Array join to get your desired output of str1.
使用.attr('id')获取它们的id并添加到数组中,然后使用Jquery的Array连接来获得所需的str1输出。
For str2 with each loop, find input with the class '.cls-quantity' and get its value. 对于每个循环的str2,找到类“.cls-quantity”的输入并获取其值。 Add that number to a list and then again use Array.join.
将该数字添加到列表中,然后再次使用Array.join。
I hope my code works fine. 我希望我的代码工作正常。 Haven't debugged it.
没有调试过。
function store_value_as_string(){
var array1= new Array();
var array2 = new Array();
$('td.edit_td1').each(function(index, element){
//Not sure if we need this line.
//Might need if we dont get the element in function's argument
//var element = this;
array1.push($(element).attr('id'));
//find might return an array so can use this line. I have not debugged it.
array2.push($(element).find('input.cls-quantity')[0].val())
//array2.push($(element).find('input.cls-quantity').val());
});
var str1 = array1.join(',');
var str2 = array2.join(',');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.