通过 jQuery 创建的文本区域 - 但不可编辑,为什么?

发布于 2024-12-21 23:50:51 字数 877 浏览 2 评论 0原文

我有这个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

或十年 2024-12-28 23:50:51

我认为问题就在这里,

 jQuery('#noticia_'+id).html(form);

你应该使用

 jQuery('#noticia_'+id).replaceWith(form);

我在这里创建了一个简化的小提琴 http://jsfiddle.net/dzytP/

一般来说,您应该将元素附加到 DOM,而不是更改 html,以便在动态添加时使

等内容起作用

I think that the problem is here

 jQuery('#noticia_'+id).html(form);

you should use

 jQuery('#noticia_'+id).replaceWith(form);

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

音盲 2024-12-28 23:50:51

更新

使用 .replaceWith() 代替,参见 jsfiddle 进行比较。

我尝试了多种方法来解决这个问题,我能想到的最好方法是定期(而不是动态)编写并在单击时显示它。

请参阅小提琴: http://jsfiddle.net/c4urself/tFxDs/ 了解我尝试过的所有选项

$("commentcontainer").click(function() {
    $(this).html("").next(".textareacontainer").show();
});

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

$("commentcontainer").click(function() {
    $(this).html("").next(".textareacontainer").show();
});
ㄖ落Θ余辉 2024-12-28 23:50:51

嗯..试图复制你的问题....但不能..看看这个小提琴。这就是我会做的方式..当然你仍然可以包含你的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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文