检测用户编辑时的表格单元格更改

时间:2017-12-07 21:18:04

标签: javascript jquery html django

我正在尝试检测用户输入的html表格单元格更改。当我单击表格单元格时,我可以在其中键入文本,但不会触发我的.change函数。但是,在从数据库填充数据时页面加载时,会触发每个表单元格更改更改功能。我无法理解为什么。你能帮我吗。 我的代码如下:

    <div id="table" class="table-editable">
        <table id="myTable" style="width:100%" class="table">
            <tr>
                <th>Id</th>
                <th>Link</th>
                <th>Naslov</th>
                <th>Lokacija</th>
                <th>Cijena</th>
                <th>Telefon</th>
                <th>Posjet</th>
                <th>Opis</th>
                <th>Prednosti</th>
                <th>Nedostaci</th>
                <th>Slike</th>
                <th>Select</th>
            {% for entry in entries %}
            </tr>
            <tr>
                <td>{{ forloop.counter }}</td>
                <td><a id="linkHouse_{{ forloop.counter }}" href="{{ entry.link }}">Link na oglas</a></td>
                <td contenteditable="true">{{ entry.header }}</td>
                <td contenteditable="true">{{ entry.location  }}</td>
                <td id="priceHouse_{{ forloop.counter }}" contenteditable="true">{{ entry.price  }}</td>
                <td id="contactHouse_{{ forloop.counter }}" contenteditable="true">{{ entry.contacts }}</td>
                <td id="visitedHouse_{{ forloop.counter }}" contenteditable="true">{{ entry.visited  }}</td>
                <td id="descriptionHouse_{{ forloop.counter }}" contenteditable="true">{% for line in entry.description.splitlines %} {{line}}<br> {% endfor %}</td>
                <td id="prosHouse_{{ forloop.counter }}" contenteditable="true" >{% for line in entry.pros.splitlines %} {{line}}<br> {% endfor %}</td>
                <td id="consHouse_{{ forloop.counter }}"contenteditable="true">{% for line in entry.cons.splitlines %} {{line}}<br> {% endfor %}</td>
                <td >
                    <a class="fancybox" target="_blank" data-fancybox="images" href="{% static "Pic/img_fjords_wide.jpg" %}"><img src="{% static "Pic/img_fjords_wide.jpg" %}" alt="" style="width:150px"></a>
                    <a class="hide fancybox" target="_blank" data-fancybox="images" href="{% static "Pic/img_lights_wide.jpg" %}"><img src="{% static "Pic/img_lights_wide.jpg" %}" alt=""></a>
                    <a class="hide fancybox" target="_blank" data-fancybox="images" href="{% static "Pic/img_mountains_wide.jpg" %}"><img src="{% static "Pic/img_mountains_wide.jpg" %}" alt=""></a>
                </td>
                <td style="text-align: center; vertical-align: middle;">
                    <button id="delHouse_{{ forloop.counter }}" onclick="delHouse(this.id)">Delete</button>

                    <!--<input type="checkbox" name="delete{{ forloop.counter }}">-->
                </td>
            </tr>
            {% endfor %}
        </table>
    </div>




$(document).ready(function(){
$( "#myTable td" )
  .change(function () {
        console.log("petar");
        var data = this.innerHTML;
        console.log("value="+data);
  })
  .change();

});

1 个答案:

答案 0 :(得分:1)

使用Javascript&#39; input方法。

blur方法。

&#13;
&#13;
$(document).ready(function(){
  $( "#myTable td" )
    .input(function () {
          console.log("petar");
          var data = this.innerHTML;
          console.log("value="+data);
    })
  .input();
});
&#13;
&#13;
&#13;

IE 6 & 8 and Opera Mini are not supported & IE 9 partial support,但不管怎样,它都有效!

如果您需要全球支持,请查看this主题。

所以你的代码看起来像是:

{{1}}