[英]How do I get the html from a contentEditable div to go inside of a hidden text form element?
I don't believe that you can use the input straight from a content editable div and submit it in a form, so I figured that I could just have a hidden text field and do something like this: 我不相信您可以直接使用内容可编辑div中的输入并以表单形式提交,因此我认为我可以只有一个隐藏的文本字段并执行以下操作:
var blog_post = $("#editable_div").html();
$('#hidden_text_area').val(blog_post);
Does anyone know of a way to do this? 有谁知道这样做的方法? It is not working with what I have. 它不适用于我所拥有的。 I thought that maybe the $('#hidden_text_area').val(blog_post);
我想也许是$('#hidden_text_area').val(blog_post);
needed to be triggered by an event, but I tried it with a click and it didn't work. 需要由一个事件触发,但我尝试点击它并没有用。
$('#clicker').click(function(){
$('#hidden_text_area').val(blog_post);
})
Try: 尝试:
$('#clicker').click(function(){
var blog_post = $("#editable_div").html();
$('#hidden_text_area').val(blog_post);
})
Because it does not look like your blog_post variable contains the html content when you click this button. 因为当您单击此按钮时,您的blog_post变量看起来不包含html内容。
please check alert($('#hidden_text_area')) is giving object or not . 请检查警报($('#hidden_text_area'))是否给予对象。 if it is not giving object then it has problem otherwise $('#hidden_text_area').val(blog_post); 如果它没有给对象那么它就有问题,否则$('#hidden_text_area')。val(blog_post); should work. 应该管用。
Seems to work well for me, however, please be aware that the html in the value attribute will be escaped: 似乎对我有用,但请注意,value属性中的html将被转义:
HTML: HTML:
<textarea id="content">
<p style="background-color: blue;">Testing</p>
</textarea>
<input type="button" value="hit me" id="btn" />
<div id="parent">
<input type="text" name="testinput" id="inputtest" value="" />
</div>
JS: JS:
$("#btn").click(function () {
var html_contents = $("#content").html();
$("#inputtest").val(html_contents);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.