Home > Back-end >  Can anyone help me with this template that is getting the weird value in my textbox
Can anyone help me with this template that is getting the weird value in my textbox

Time:09-17

I have the following codes. Im using Django and using raw SQL statement to send data to my template for this part.

views.py (SQL statement part)

cursor = connection.cursor()
tablename= "dev_interface_"   str(device.id)
cursor.execute(f"SELECT interface FROM {tablename} WHERE id >=2")
righttable = cursor.fetchall()

template (Where i loop the SQL)

<table class="table tablesorter">
 <thead>
  <tr>
   <th>Interface registered</th>
   <th>Check to delete</th>
  </tr>
 </thead>
 <tbody>
  {% for item in righttable %}
  <tr>                                            
   <td>{{ item.0 }}</td>
    <td>
     <div class="form-check">
      <input type="checkbox" value="{{item.0}}" class="chkcvalues">
     </div>
    </td>
  </tr>
  {% endfor %}
 </tbody>
</table>
<input type ="text" name="deleteint" id="txtvalues" >

This is how the page look like when i run it: Looping of data in SQL

Script (Coded this to add all the item that is selected for the checkbox in my deleteint)

$(document).ready(function()
    {
        $('.chkcvalues').click(function()
        {
            var txt =""
            $('.chkcvalues:checked').each(function(){
                txt =$(this).val(txt)   ","
            })
            txt=txt.substring(0,txt.length-1)
            $('#txtvalues').val(txt);
        });
    });

It looks visually fine as the data displayed are correct. But as i tick the checkbox. Within the textbox (deleteint) I declared at the end of my table, it register the value of [object Object] Instead of what i intend it to be. Example if i tick the checkbox of TenGigabitEthernet1/0/5. That should appear in my textbox. And as shown in my code, the checkbox are assign with value="{{item.0}}". Can anyone explain this part to me as I do not get it how {{item.0}} works for the first column but doesnt work in the checkbox.

CodePudding user response:

I try to rebuild your code. Now it works.

$(document).ready(function() {
  $('.chkcvalues').click(function() {
    var txt = ""
    $('.chkcvalues:checked').each(function(i, v) {
      txt  = v.value   ","
    })
    txt = txt.substring(0, txt.length - 1)
    $('#txtvalues').val(txt);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
  <thead>
    <tr>
      <th>Interface registered</th>
      <th>Check to delete</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>TenGigabitEthernet1/0/5</td>
      <td>
        <div class="form-check">
          <input type="checkbox" value="{{item.0}}" class="chkcvalues">
        </div>
      </td>
    </tr>
    <tr>
      <td>TenGigabitEthernet1/0/6</td>
      <td>
        <div class="form-check">
          <input type="checkbox" value="{{item.1}}" class="chkcvalues">
        </div>
      </td>
    </tr>
    <tr>
      <td>TenGigabitEthernet1/0/7</td>
      <td>
        <div class="form-check">
          <input type="checkbox" value="{{item.2}}" class="chkcvalues">
        </div>
      </td>
    </tr>
  </tbody>
</table>
Checkbox-Value: <input type ="text" name="deleteint" id="txtvalues" >

CodePudding user response:

I had to correct txt =$(this).val(txt) "," to txt =$(this).val() "," and it worked perfectly

  • Related