Textarea到段落或纯文本

时间:2013-02-13 22:17:10

标签: php jquery textarea paragraph

我有一个表单,用户可以通过textarea进行评论。如果单击提交按钮,则返回在textarea内的textarea中输入的值。如何更改它以便在提交时textarea的值以jquery或php的纯文本/段落返回?

textarea和按钮的代码:

<textarea rows="6" cols="40" scroll="auto" name="reply_for_{$item.id}">
    {if isset($item.reply)}
        {$item.reply}
    {/if}
</textarea>
<br />
<input name="addcomment" value="{lang mkey='submit'}" type="button" onclick="
    document.forms['commentsFrm'].id.value={$item.id};
    document.forms['commentsFrm'].submit(); 
"/>

3 个答案:

答案 0 :(得分:0)

$('input[name="addcomment"]').on('click',function(){
     var comment = $('textarea').val();
     $('textarea').remove(); // removes the textarea
     var actContent = $(this).parent().html(); // get the actual content of the parent dom node.
     $(this).parent.html('<p>'+comment+'</p>'+actContent); // you set the new html content for the parent dom node

});

因此,这将采用textarea内容并将其保存为段落。有点尴尬的解决方案,但它应该做的神奇。

答案 1 :(得分:0)

有很多方法可以做到这一点,但这就是我想到的:

$('input[name="addcomment"]').on('click', function() {
    var textArea = $('textarea[name="reply_for_{$item.id}"]');
    var text = textArea.val();
    $('<div id="no-editing-me">').text(text).insertAfter(textArea);
    textArea.hide();
});

如果您提供元素ID,则不必担心jQuery中的所有[name="reply_for_{$item.id}"] gunk。它也会更快。

答案 2 :(得分:0)

2个答案部分解决了问题。然而它给了我一些新的灵感,我就这样解决了它

{if $item.reply == ''}
<form name="reply" method="post" action="reply">
    <textarea id="txtreply" name="txtreply" cols="50" rows="5"></textarea>
    <input type="submit" name="btnAdd" value="{lang mkey='send'}" />
</form>

{else}

{$item.reply}

{/if}

感谢大家的帮助