脚本可以在控制台中工作,但不能在原始代码中工作

时间:2019-05-07 14:19:09

标签: javascript jquery html

$('#edit-post').modal('hide');在控制台中有效,但在脚本文件中无效。除非刷新页面,否则我也看不到新信息。提前致谢! (下面的评论部分中建议的解决方案。)

脚本文件:

var postId = 0;
var postBodyElement = null;

$('.post').find('.interaction').find('.edit').on('click',function(event){
    event.preventDefault();

    postBodyElement = event.target.parentNode.parentNode.childNodes[1];
    var postBody = postBodyElement.textContent;
    //postId = event.target.parentNode.parentNode.dataset['postid'];
    postId = event.target.dataset.postid;
    $('#post-content').val(postBody);
    $('#edit-post').modal();
});

$('#modal-save').on('click',function(){
    $.ajax({
        method: 'POST',
        url: url,
        data: {body: $('#post-content').val(), postId: postId, _token: token}
    })
        .done(function (msg){
            //console.log(msg['message']);
            //console.log(JSON.stringify(msg));
            $(postBodyElement).text(msg['new_content']);
            $('#edit-post').modal('hide');
        })
});

使用以下命令将文件加载到主HTML中:

...

</head>
<body>
    @include('includes.header')
    <div class="container">
        @yield('content')
    </div>

    <!--<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>-->
    <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src={{ URL::to('src/js/app.js') }}></script>
</body>
</html>

在模式中点击“保存”按钮后,它应该自动关闭,并且页面应该反映新数据。

0 个答案:

没有答案