简体   繁体   English

我如何通过单击使用jquery提交来发布文本区域的内容

[英]How do I post text area's content by click submit using jquery

<div id="wrapper">
        <div id="header">
            <h1 style="color:#000000"> WELCOME TO TROLL CONTROLL </h1>
        </div>

  <div id="content">
            <textarea name="tmnl" id= "tmnl" cols="50" rows="10" placeholder="Please Share Your Story" > </textarea>
            <br/>
            <input type="submit" name= "tmnlsubmit" id="tmnlsubmit" />

            <script> 
            var text = $('#tmnl').val(); 
            $('#tmnlsubmit').click(function(){
            $('#content').append(
                '<div class="post"> 
                </div> ');
                $('#content .post').html(<h2> + Post Title + </h2>
                        <p> + text + </p>);
                                            });
            </script>
      </div>
  </div>
  <script
  src="http://code.jquery.com/jquery-3.1.1.min.js"
  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  crossorigin="anonymous"></script>  
</body>

I would like to be able be able to post the content of the textarea into the "content" div by creating a new div with each post. 我希望能够通过在每个帖子中创建一个新的div来将textarea的内容发布到“ content” div中。 I tried everything and everytime I press submit nothing happens. 我尝试了一切,每按一次提交,都不会发生任何事情。 Please help! 请帮忙!

All are good. 一切都很好。 post_Title variable was undeclared. post_Title变量未声明。 And apply $(.post) is enough to insert html data.better to use $(document).ready(function (){} 并应用$(.post)足以插入html data。更好地使用$(document).ready(function (){}

 $(document).ready(function () { $('#tmnlsubmit').click(function(){ var Post_Title ="Post Title" var texts = $('#tmnl').val(); $('#content').append('<div class="post"></div> '); $('.post').html('<h2>' +Post_Title+' </h2><p> '+ texts +' </p>'); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <div id="header"> <h1 style="color:#000000"> WELCOME TO TROLL CONTROLL </h1> </div> <div id="content"> <textarea name="tmnl" id= "tmnl" cols="50" rows="10" placeholder="Please Share Your Story" > </textarea> <br/> <input type="submit" name= "tmnlsubmit" id="tmnlsubmit" /> <p class="post"></p> </div> </div> 

There are so many issues with your code. 您的代码有太多问题。 Make following changes: 进行以下更改:

  <div id="content">
            <textarea name="tmnl" id= "tmnl" cols="50" rows="10" placeholder="Please Share Your Story" > </textarea>
            <br/>
            <input type="submit" name= "tmnlsubmit" id="tmnlsubmit" />
      </div>
  </div>

$('#tmnlsubmit').click(function(){
    var text = $('#tmnl').val();
  $('#content').append('<div class="post"></div>');
  $('#content').html('<h2>Post Title</h2><p>'+ text +'</p>');
});

Working Fiddle 工作小提琴

  • You have missed some quotes. 您错过了一些报价。
  • To avoid repeat post to all .post use .append(div + h2 + p) . 为了避免对所有.post重复发布,请使用.append(div + h2 + p)
  • I think you need to .prepend() to show new posts first. 我认为您需要.prepend()才能首先显示新帖子。

 input,textarea{ display:block; margin:5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <div id="header"> <h1 style="color:#000000"> WELCOME TO TROLL CONTROLL </h1> </div> <div id="content"> <input id='title' name='title' placeholder='Title' /> <textarea name="tmnl" id="tmnl" cols="50" rows="10" placeholder="Please Share Your Story"></textarea> <br/> <input type="submit" name="tmnlsubmit" id="tmnlsubmit" /> <script> $('#tmnlsubmit').click(function() { var text = $('#tmnl').val(); var title = $('#title').val(); //use .prepend() instead of append to show new posts first $('#content').append('<div class="post">' + '<h2>' + title + '</h2><p>' + text + '</p>' + '</div>'); //clear textarea $('#tmnl, #title').val(""); }); </script> </div> </div> 

hi please review this code 您好,请查看此代码

  $('#tmnlsubmit').on('click', function(e) { var text = $('#tmnl').val(); alert(text); $('#content').append("<div class='post'></div>"); $('#content .post').html("<h2>Post Title</h2> <p> "+text +" </p>"); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <div id="header"> <h1 style="color:#000000"> WELCOME TO TROLL CONTROLL </h1> </div> <div id="content"> <textarea name="tmnl" id="tmnl" cols="50" rows="10" placeholder="Please Share Your Story"> </textarea> <br/> <input type="submit" name="tmnlsubmit" id="tmnlsubmit" /> </div> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用提交按钮创建一个文本框,使用js或jQuery单击javascript函数执行回调? - How do I create a text box with a submit button that performs a callback on click to a javascript function using js or jQuery? 如何在单击按钮时更改div的文本内容? 香草JS / jQuery - How do I change a div's text content on button click? Vanilla JS/Jquery 如何使用jQuery发布/提交表单? - How do i POST/Submit a form using Jquery? 如何在默认情况下禁用提交按钮,直到文本区域中键入1个字符或更多字符? - How do I disable a submit button by default until it's text area has 1 character or more typed into it? 如何使用js或jquery或php在文本区域中显示格式化的html内容? - How can I show formatted html content in text area using js or jquery or php? 单击提交按钮时如何仅加载内容区域? - How to load only the content area when click submit button? 如何在使用jQuery类型提交的按钮上收听点击事件? - How do I listen to click events on buttons with type submit using jQuery? jQuery:如何将点击侦听器与Submit事件功能结合在一起? - Jquery: How do I combine a click listener with a submit event function? 如何使用jquery启用表单的提交按钮? - How do I enable my form's submit button using jquery? 如何使用类名称通过Jquery在文本区域内获取文本内容 - How to get the text content inside a Text Area with Jquery using its class name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM