Quoting through an HTML table and saving td values ​​as a string using jquery / javascript

I have the 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 the 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();after td_validation();, which goes through everything <tr>and gets idfrom class="edit_td1"and the associated value id="quanti_<?php echo $id; ?>". Then the function store_value_as_string();will give me two lines

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 lines are needed to call ajax (go to another php page). Actually, I have the code for the same, but it starts onchangefrom ".edit_td1", but the sequence of operations using → tester <- ie onchange-blur(leave the textbox for same <tr> -onchange-blure-onchange...gives me the wrong output.

The table looks like: enter image description here

+4
1

Jquery . td '.edit_td1'. , .attr('id'), , , Jquery Array join, str1.

str2 ".cls-quantity" . , Array.join.

, . .

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 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(',');
}
+1

Source: https://habr.com/ru/post/1606709/


All Articles