【问题标题】:Textarea to paragraph or plain text文本区域到段落或纯文本
【发布时间】:2013-01-29 15:10:31
【问题描述】:

我有一个表单,用户可以通过文本区域进行评论。如果单击提交按钮,它将返回在文本区域内的文本区域中输入的值。我该如何更改它,以便在提交时将 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(); 
"/>

【问题讨论】:

    标签: php jquery textarea paragraph


    【解决方案1】:
    $('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
    
    });
    

    所以这将获取文本区域内容并将其保存到一个段落中。有点尴尬的解决方案,但它应该可以发挥作用。

    【讨论】:

    • 嗨,在页面完全刷新之前,这两个答案似乎都有效。文本区域消失了一会儿,然后又回来了。但是我确实通过在页面中添加 {if} {/if} 语句找到了解决方案。因此,当回复 = 空时,它会显示一个文本区域或回复的值。所以这解决了我的问题。不是我正在寻找的方式,但它工作正常。感谢您的帮助。
    【解决方案2】:

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

    $('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。它也会更快。

    【讨论】:

      【解决方案3】:

      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}
      

      感谢大家的帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-05
        • 2021-08-14
        • 2020-05-10
        • 2014-11-11
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多