2017-04-23 87 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