繁体   English   中英

如何在html td id中使用javascript变量

[英]How to use a javascript variable within an html td id

为了检查我的表中的单元格是否包含javascript中的值,似乎我需要给每个td一个id。 我将拥有数千个单元格,因此无法输入每个td的id。

目前所有tds的trs都是用html中的循环生成的,所以使用javascript我想添加一个变量并将其粘贴在每个id的末尾。

我已经设法将一个javascript变量放到我的html循环中并且它正确计数,但我的问题是将它变成id =“___”部分。

如下面的代码所示,我已经尝试将document.write(i)行放入id中,但它似乎将它视为普通文本。 我还把它放在DataEntryStatus的输出中,只是为了证明它正确计数,从1开始并随每个新行增加。

<table class="table" id="table_id">
    <tr>
        <th style="color:#ddd">fffffffffffffff<br>f<br>f<br>f</th>
        <th style="vertical-align:bottom;">Data Entry Status</th>
        <th style="vertical-align:bottom;">Tool</th>
    </tr>


<tbody id="myTable">

<script>
    var i = 1;    
</script>
{% for item in items %}
    <tr>
        <td>
            <a href="{% url 'edit_newcuts' item.pk %}" class="btn btn-primary btn-sm" role="button">Edit</a>
            <a href="{% url 'delete_newcuts' item.pk %}" class="btn btn-danger btn-sm" role="button">! X !</a>
        </td>

        <td id="DataEntryStatus"><div>{{ item.DataEntryStatus }} <script>document.write(i)</script></div></td>
        <td id="Tool + <script>document.write(i)</script>"><div>{{ item.Tool }}</div></td>
    </tr>

<script>
    i = i + 1;    
</script>
{% endfor %}
</tbody>

而我的javascript:

    $('#table_id tr').each(function(){
    if($('#Tool' + 'i').text() =="")$('#DataEntryStatus' + 'i').text("Entry missing");
    else if($('#CutRef' + 'i').text() =="")$('#DataEntryStatus' + 'i').text("Entry missing");
    else($('#DataEntryStatus' + 'i').text("Complete"));


    if($(this).text().toLowerCase() =="entry missing")$("#DataEntryStatus").css('background-color','#fcc');
    if($(this).text().toLowerCase() =="complete")$("#DataEntryStatus").css('background-color','#af0');
});

我想要像我的id =“Tool + document.write(i)”这样的行来制作像Tool1,Tool2一样的ID,但是现在它正在将+ document.write(i)视为普通文本,我不知道如何让它作为一个脚本工作。

看起来你正在使用Django,那么为什么不使用它添加ID呢?

<table class="table" id="table_id">
    <tr>
        <th style="color:#ddd">fffffffffffffff<br>f<br>f<br>f</th>
        <th style="vertical-align:bottom;">Data Entry Status</th>
        <th style="vertical-align:bottom;">Tool</th>
    </tr>


<tbody id="myTable">

{% for item in items %}
    <tr>
        <td>
            <a href="{% url 'edit_newcuts' item.pk %}" class="btn btn-primary btn-sm" role="button">Edit</a>
            <a href="{% url 'delete_newcuts' item.pk %}" class="btn btn-danger btn-sm" role="button">! X !</a>
        </td>

        <td id="DataEntryStatus{{ forloop.counter0 }}"><div>{{ item.DataEntryStatus }}</div></td>
        <td id="Tool{{ forloop.counter0 }}"><div>{{ item.Tool }}</div></td>
    </tr>

{% endfor %}
</tbody>

Django在循环内部有一个forloop变量 ,可以访问当前索引。

更新

要将其与JavaScript一起使用, counter0 counter更改为counter0 现在这与JavaScript循环中的索引相同。

你可以通过循环使用.each来访问它,但是稍作修改。

$('#table_id tr').each(function(i){
    if($('#Tool' + i).text() =="")$('#DataEntryStatus' + i).text("Entry missing");
    else if($('#CutRef' + i).text() =="")$('#DataEntryStatus' + i).text("Entry missing");
    else($('#DataEntryStatus' + i).text("Complete"));


    if($(this).text().toLowerCase() =="entry missing")$("#DataEntryStatus").css('background-color','#fcc');
    if($(this).text().toLowerCase() =="complete")$("#DataEntryStatus").css('background-color','#af0');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM