2017-04-23 56 views
1

我在我的樹枝模板中有一個窗體,其中包含一個可滿足的跨度。我想獲得新的文本並將其附加到span,所以當表單被submited時它包含新文本。如何獲得滿意的跨度文本?

這裏是我的HTML表單:

<form action="{{ path('change') }}" method="post"> 
    <span name="profil" class="profil" contenteditable="true">{{ prof.libel }}</span> 
    <button name="change" type="submit"> 
    </button> 
</form> 

這是我的jQuery腳本:

<script> 
    $(document).ready(function() { 
     $('span.profil').change(function() { 
      $text = $(this).text(); 
      $(this).append(document.createTextNode($text)); 
     }); 
    }); 
</script> 
+0

您是否知道您的HTML無效?你打開的按鈕元素缺少一個關閉角度支架 –

+0

啊好吧,這是因爲我發佈此問題時刪除了一些代碼。但在我的項目中,我的代碼沒有遺漏任何東西。 – Susan

回答

1

span元素有沒有這樣的change事件。您應該在BUTTON的click事件或FORM的submit事件中捕獲範圍的文本,例如,

$('#theForm').on("submit", function() { 
    var text = $('span.profil').text(); 
    // or $('span.profil')[0].innerHTML; 
}); 

// or... 
$('#theButton').on("click", function() { 
    var text = $('span.profil').text(); 
}); 

參考:本divspan,和其他人實現不具有對change事件的交互HTMLElement接口。檢查支持處理change event的元素。

+0

如果我改變跨度到div將它工作?因爲我看到很多div示例 – Susan

+0

['div'](https://html.spec.whatwg.org/multipage/semantics.html#the-div-element),['span'](https:// html .spec.whatwg.org/multipage/semantics.html#the-span-element),而其他實現了** ['HTMLElement'](https://html.spec.whatwg.org/multipage/dom.html# htmlelement)**這個接口沒有'change'事件的交互。檢查支持處理['change'事件]的元素(https://developer.mozilla.org/en-US/docs/Web/Events/change) – jherax

+0

我認爲這不適用於我的情況,因爲我有動態創建的按鈕和跨度元素,所以我怎樣才能確定哪個跨度和哪個按鈕工作? – Susan