weixin_33721344 2020-03-17 19:05 采纳率: 0%
浏览 29

AJAX按钮消失

I have a web app in laravel and I have a simple textarea with a form that allows users to enter some basic markdown text and I need it to output the data in both HTML tags and then normal formatting, my main problem, is my button disappears after submitting the first time and I can only seem to have it print out the HTML formatting or the text values but not both at the same time. I'm a little new to AJAX so any help would be awesome. Here is my AJAX code and part of my form.

<form action="{{action('MarkDownController@process')}}" method="post" name="markdownform" id="markdownform">
    <input name="_token" type="hidden" value="{{ csrf_token() }}"/>
    <input type="button" id="ctaPrimary" value="Parse"/>
</form>

<script>
      $(function () {

        $('#ctaPrimary').click(function(e) {

          e.preventDefault();

          $.ajaxSetup({headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }});

          $.ajax({
            type: 'post',
            url: 'process',
            crossDomain:'true',
            data: $('#markdownform').serialize(),
            success: function (data) {
                $("#markdownform").html(data);
            }
          });

        });

      });
    </script>

So I was able to fix my problem overall, by changing

data: $('#markdownform').serialize(),
            success: function (data) {
                $("#markdownform-html").html(data);
                $("#markdownform-text").text(data);
            }

And just adding:

<div id="markdownform-html"></div>
<div id="markdownform-text"></div>

I guess my final question is, with $("#markdownform-text").text(data); I would like the data to show on a new line if there is a space between the elements not how it is showing below, in just one long block:

<h1>Header one</h1> <p>Hello</p> <p>more text <br />What's going on?</p> <h2>Another Header</h2> <p>something hear, eh?</p>
  • 写回答

1条回答 默认 最新

  • weixin_33688840 2020-03-17 19:26
    关注

    You are replacing the content that is in the <form> tags, you need to put it into a separate <div> to ensure it does no change the HTML within it.

    <form action="{{action('MarkDownController@process')}}" method="post" name="markdownform" id="markdownform">
        <input name="_token" type="hidden" value="{{ csrf_token() }}"/>
        <input type="button" id="ctaPrimary" value="Parse"/>
    </form>
    <div id="markDownContent"></div>
    
    <script>
        $(function () {
            $('#ctaPrimary').click(function(e) {
                e.preventDefault();
    
                $.ajaxSetup({
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
                });
    
                $.ajax({
                    type: 'post',
                    url: 'process',
                    crossDomain:'true',
                    data: $('#markdownform').serialize(),
                    success: function (data) {
                        $("#markDownContent").html(data);
                    }
                });
            });
        });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan