自动显示' div'在隐藏了另一个' div'在javascript

时间:2018-06-10 10:28:47

标签: javascript jquery html

我有一个user question form,当我使用ajax提交表单并保存到数据库时,会显示一条成功的消息,几秒钟后此消息将消失,user question form将自动显示再次显示。

我能够消失成功的消息,但无法再自动显示user question form

这是我的html标记:

<div class="so-widget-sow-editor so-widget-sow-editor-base question-modal">

    <div class="siteorigin-widget-tinymce textwidget">
        <h3>Ask Question</h3>
        <form>
            <div class="form-group">
                <input type="text" id="question_form_name" class="form-control" id="exampleInputPassword1" placeholder="Your name">
                <div id="question_form_name_error_message" class="val_error"></div>
            </div>

            <div class="form-group">
                <textarea class="form-control" id="question_form_text_area" rows="3" placeholder="Ask your question" style="height:10em;"></textarea>
                <div id="question_form_text_area_error_message" class="val_error"></div>
            </div>

            <button type="button" class="btn btn-primary" id="question_form_button">Submit</button>

        </form>
    </div>
</div>

这是我的Javascript代码:

$.ajax({

            url:"{{ url('/ask_question_form') }}",
            type: 'GET',
            data: {_token :token, name : name, email : email, contact : contact, question : question},
            success:function(msg){

                // console.log(msg);

                $('.question-modal .textwidget').hide();

                trHTML = "";
                trHTML += "<div id='user-question' style='margin-top:50%; color:#0071BC'>";

                trHTML += "Thanks for your question. We will save your question for further query and give a feedback as soon as possible.";


                trHTML += "</div>";

                $('.question-modal').append(trHTML);

                setTimeout(function() {
                    $('#user-question').hide();
                }, 10000);

                $('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​//----this part is not working
            }
        });

2 个答案:

答案 0 :(得分:1)

将show再次代码放在setTimeout()

更改以下代码

setTimeout(function() {
    $('#user-question').hide();
}, 10000);

$('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​

setTimeout(function() {
    $('#user-question').hide();
    $('.question-modal .textwidget').show();​​​​​​
}, 10000);

答案 1 :(得分:0)

保持这一行

$('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​

超时

            setTimeout(function() {
                $('#user-question').hide();
                $('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​//----this part is not working
            }, 10000);

$。AJAX({

        url:"{{ url('/ask_question_form') }}",
        type: 'GET',
        data: {_token :token, name : name, email : email, contact : contact, question : question},
        success:function(msg){

            // console.log(msg);

            $('.question-modal .textwidget').hide();

            trHTML = "";
            trHTML += "<div id='user-question' style='margin-top:50%; color:#0071BC'>";

            trHTML += "Thanks for your question. We will save your question for further query and give a feedback as soon as possible.";


            trHTML += "</div>";

            $('.question-modal').append(trHTML);

            setTimeout(function() {
                $('#user-question').hide();
                $('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​//----this part is not working
            }, 10000);


        }
    })