通过 jQuery 创建的文本区域 - 但不可编辑,为什么?
我有这个简单的 HTML 代码...
<div onclick='jsComment();'>Click to comment</div>
jsComment()
函数只是创建一个带有文本区域和按钮的表单,并替换 div 的 HTML 内容
function jsComment(id){
var form = '<form action="modules.php?name=some_url&id='+id+'" method="post" name="post">';
form += '<b>Post something!</b><br />';
form += '<textarea name="noticia" cols="100" rows="5" maxlength="299" style="overflow: hidden; border: none; border-top: 1px solid LightGrey; margin: 6px;"></textarea><br />';
form += '<input type="hidden" name="noticiaId" value="'+id+'" /><input type="submit" name="op" value="Comment!"></form>';
jQuery('#noticia_'+id).html(form);
}
HTML 已正确生成,但我无法在文本区域。当我将光标放在文本区域内时,它就消失了...提交按钮也是如此,它似乎被禁用了...这里出了什么问题?也许是 CSS 问题?
非常感谢
I have this simple HTML code...
<div onclick='jsComment();'>Click to comment</div>
And the jsComment()
function just creates a form with a textarea and a button and replaces HTML content of the div
function jsComment(id){
var form = '<form action="modules.php?name=some_url&id='+id+'" method="post" name="post">';
form += '<b>Post something!</b><br />';
form += '<textarea name="noticia" cols="100" rows="5" maxlength="299" style="overflow: hidden; border: none; border-top: 1px solid LightGrey; margin: 6px;"></textarea><br />';
form += '<input type="hidden" name="noticiaId" value="'+id+'" /><input type="submit" name="op" value="Comment!"></form>';
jQuery('#noticia_'+id).html(form);
}
HTML is generated correctly, but I cannot write in the textarea. When I place the cursor inside the textarea it just disappears... same for the submit button, it appears to be disabled... what's going wrong here? Maybe a CSS question?
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为问题就在这里,
你应该使用
我在这里创建了一个简化的小提琴 http://jsfiddle.net/dzytP/
一般来说,您应该将元素附加到 DOM,而不是更改 html,以便在动态添加时使
I think that the problem is here
you should use
i created a simplified fiddle here http://jsfiddle.net/dzytP/
Generally speaking, you should append elements to the DOM and not change the html to have things like
<FORM>
work when added dynamically更新
使用 .replaceWith() 代替,参见 jsfiddle 进行比较。
我尝试了多种方法来解决这个问题,我能想到的最好方法是定期(而不是动态)编写并在单击时显示它。
请参阅小提琴: http://jsfiddle.net/c4urself/tFxDs/ 了解我尝试过的所有选项
Update
Use .replaceWith() instead, see jsfiddle for comparison.
I tried multiple ways to fix this the best way I could come up with is to write the regularly (not dynamically) and show it onclick.
See the fiddle: http://jsfiddle.net/c4urself/tFxDs/ for all the options I tried
嗯..试图复制你的问题....但不能..看看这个小提琴。这就是我会做的方式..当然你仍然可以包含你的ID.. http://jsfiddle.净/pixelass/CpmvV/
ehm.. tried to replicate your problem.... but couldn't.. look at this fiddle. This is the way I would do it.. of course you can still include your IDs.. http://jsfiddle.net/pixelass/CpmvV/