仅当变量具有使用jquery的值时才在html中显示逗号?

时间:2013-04-02 10:19:31

标签: jquery html

我有一张这样的表:

<table class="table table-bordered">  
        <thead>  
          <tr>  
            <th></th>
            <th>Project Name</th>
            <th>Tape-ID</th>  
            <th>Video Type</th>  
            <th>Date Created</th>  
            <th>Director</th>  
            <th>Camera Person</th>
            <th>Editor</th> 
            <th>Tag</th> 
          </tr>  
        </thead>  
        <tbody>  
        {% load endless %}

    {% paginate 8 a %}
        {% for choice, i in a %}
          <tr>  
            <td><input type="checkbox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"  onchange="checkChecked()" /></td>
            <td>{{ choice.name }}</td>
            <td>{{ choice.tape_id }} </td>  
            <td>{{ choice.video_type }}</td> 
            <td>{{ i }}</td>  
            <td>{{ choice.director }}</td>  
            <td>{{ choice.cameraman }}</td>  
            <td>{{ choice.editor }}</td> 
            <td>{{ choice.tag1 }}, {{ choice.tag2 }}, {{ choice.tag3 }}, {{ choice.tag4 }},
            {{ choice.tag5 }}, {{ choice.tag6 }}, {{ choice.tag7 }}, {{ choice.tag8 }}, 
            {{ choice.tag9 }}, {{ choice.tag10 }}


            </td>   
          </tr>  
       {% endfor %}

        </tbody>  
      </table>  

这些变量{{ choice...}}可能有也可能没有价值。虽然这是顺序的。变量up3可以有值,3之后没有值。如果没有值,它就像这样:

water, pollution, hello, , , , , ,

如果没有价值,我不想显示逗号。我怎么能这样做?

4 个答案:

答案 0 :(得分:0)

如果你正在寻找一个jQuery解决方案(看到你标记为jQuery),你可以做这样的事情:

$(function() {
    // get rows
    $('table.table tbody tr').each(function() {
        // get last cell in each row
        var cell = $(this).find('td:last');
        // replace trailing spaces and commas
        var text = cell.html().replace(/(,|\s)+$/, '');
        // update cell display
        cell.html(text);
    });
});

概念证明:http://jsfiddle.net/wPRNH/

答案 1 :(得分:0)

获得结果s = s.replace(/[,\s]{2,}/,"");

后可以使用正则表达式

答案 2 :(得分:0)

试试这个:

  <td>
  {% if choice.tag1 %}
      {{ choice.tag1 }}, 
  {% endif %} 
  {% if choice.tag2 %}
      {{ choice.tag2 }}, 
  {% endif %} 
  {% if choice.tag3 %}
      {{ choice.tag3 }}, 
  {% endif %} 
  {% if choice.tag4 %}
      {{ choice.tag4 }}, 
  {% endif %}
  {% if choice.tag5 %}
      {{ choice.tag5 }}, 
  {% endif %}
  {% if choice.tag6 %}
      {{ choice.tag6 }}, 
  {% endif %}
  {% if choice.tag7 %}
      {{ choice.tag7 }}, 
  {% endif %} 
  {% if choice.tag8 %}
      {{ choice.tag8 }}, 
  {% endif %} 
  {% if choice.tag9 %}
      {{ choice.tag9 }}, 
  {% endif %} 
  {% if choice.tag10 %}
      {{ choice.tag10 }} 
  {% endif %} 

   </td>  

或者使用jquery尝试这个:

     $(document).ready(function () {
        $("#add_commas .manupulate_me span:not(span:last-child)").each(function ()  {
            if ($.trim($(this).html()) != "") {
                $(this).append(",");
            }
        });
    });

并将更改改为html:

<table id="add_commas">
<tr class="manupulate_me">
<td>
    <span id="sp1">value1</span>
    <span id="sp2">vale2</span>
    <span id="sp3"> </span>
    <span id="sp4"> </span>
    <span id="sp5"> </span>
    <span id="sp6">value6</span>
    <span id="sp7">value7</span>
    <span id="sp8"> </span>
    <span id="sp9">value9</span>
    <span id="sp10"> value10</span>
  </td>  
</tr>
</table>

答案 3 :(得分:0)

尝试使用

{{#choice.tag1}}
  {{ choice.tag1 }}{{#choice.tag2}},{{/choice.tag2}}
{{/choice.tag1}}

每次出现。最后一个是:

{{#choice.tag10}}
  {{ choice.tag10 }}
{{/choice.tag10}}