2011-09-08 9 views
21

¿Se puede usar <div contenteditable="true"><a href="page.html">Some</a> Text</div> en lugar de texarea y luego pasar a través de la forma de alguna manera?¿Se puede pasar div con contenteditable = true a través de la forma?

idealmente sin JS

+1

posible duplicado de [Usando HTML5, ¿cómo utilizo los campos contenteditable en un envío de formulario?] (Http://stackoverflow.com/questions/6247702/using-html5-how-do-i-use-contenteditable-fields -in-a-form-submission) – dbn

Respuesta

18

Using HTML5, how do I use contenteditable fields in a form submission?

contenido editable no funciona como un elemento de formulario. Solo javascript puede permitir que funcione.

EDITAR: Respondiendo a tu comentario ... Esto debería funcionar.

<script type="text/javascript"> 
    function getContent(){ 
     document.getElementById("my-textarea").value = document.getElementById("my-content").innerHTML; 
    } 
</script> 


<div id="my-content" contenteditable="true"><a href="page.html">Some</a> Text</div> 

<form id="form" action="some-page.php" onsubmit="return getContent()"> 
    <textarea id="my-textarea" style="display:none"></textarea> 
    <input type="submit" /> 
</form> 

He probado y verificado que esto funciona en FF e IE9.

+0

¿Qué debo hacer con JS (no jquerry) para que funcione? \t \t \t \t document.getElementById ('contentTextArea'). Value = document.getElementById ('contentDiv'). Value; esto no funciona – JDDll

+0

Hmm eso es cierto, los div no tienen un atributo de "valor". Actualizaré mi respuesta en un momento. – smdrager

1

probar este

document.getElementById('formtextarea').value=document.getElementById('editable_div').innerHTML; 

un ejemplo completo: -

<script type="text/javascript"> 
    function getContent(){ 
var div_val=document.getElementById("editablediv").innerHTML; 
     document.getElementById("formtextarea").value =div_val; 
    if(div_val==''){ 
    //alert("option alert or show error message") 
    return false; 
    //empty form will not be submit. You can also alert this message like this. 
    } 
     }</script> 

`

<div id="editablediv" contenteditable="true"> 
<a href="page.html">Some</a> Text</div> 
<form id="form" action="action.php" onsubmit="return getContent()"> 
    <textarea id="formtextarea" style="display:none"></textarea> 
    <input type="submit" /> 
</form> 

`

en lugar de ello se puede utilizar jQuery (si hay una inundación para usar JQuery para área de texto de tamaño automático o cualquier editor de texto WYSIWYG)

0

usted podría utilizar mejor:

<script type="text/javascript"> 
    function getContent(){ 
     document.getElementById("my-textarea").value = document.getElementById("my-content").innerText; 
    } 
</script> 

NOTA: He cambiado .innerHTML a innerText. De esta forma, no obtendrá elementos HTML y texto, sino solo texto.

Ejemplo: Envié "texto", innerHTML da el valor: "\ r \ n texto". Filtra el "texto" pero es más largo que 4 caracteres. innerText da el valor "texto".

Esto es útil si desea contar los caracteres.

Cuestiones relacionadas