【问题标题】:Can anyone help me with this template that is getting the weird value in my textbox谁能帮我这个模板在我的文本框中得到奇怪的值
【发布时间】:2021-09-09 08:22:53
【问题描述】:

我有以下代码。我使用 Django 并使用原始 SQL 语句将数据发送到我的模板。

views.py(SQL 语句部分)

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

模板(我循环 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" >

这是我运行页面时的样子:

脚本(编写此代码以添加在我的 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);
        });
    });

由于显示的数据正确,因此在视觉上看起来不错。但是当我勾选复选框时。在我在表格末尾声明的文本框 (deleteint) 中,它注册了 [object Object] 的值,而不是我想要的值。例如,如果我勾选 TenGigabitEthernet1/0/5 的复选框。那应该出现在我的文本框中。如我的代码所示,复选框被赋值为“{{item.0}}”。谁能向我解释这部分,因为我不明白 {{item.0}} 如何在第一列中起作用,但在复选框中不起作用。

【问题讨论】:

    标签: javascript html jquery sqlite django-views


    【解决方案1】:

    我尝试重建您的代码。现在可以了。

    $(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" >

    【讨论】:

    • 如果我的代码符合您的需求,请投票或检查是否已回答。谢了。
    • 感谢您抽出宝贵时间查看和回答!这是一个很好的例子,我很乐意为将来面临麻烦的任何人投票,但我的声誉还不够。但我只是在一分钟前设法解决了它。似乎是txt+=$(this).val(txt) + "," 这句话给我带来了麻烦。所以我把.val()留空了,它起作用了。
    【解决方案2】:

    我必须将txt+=$(this).val(txt) + "," 更正为txt+=$(this).val() + ",",它运行良好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      相关资源
      最近更新 更多