如何在onclick之后附加下一个div?

时间:2019-05-03 10:57:56

标签: javascript jquery html append

我正在尝试在chatUserSend()函数之后追加下一个div,但是当我在上述函数之后放置新的append时,即使尚未单击chatUserSend(),显示的div也没有被点击< / p>

该如何解决?

请参阅更新的JS脚本。

var custLoanAccountNum = '<span id="custLoanAccountNum" class="chat_msg_item chat_msg_item_admin"><p>What is your Loan Account Number?</p></span>';
var custName = '<span id="custConcern" class="chat_msg_item chat_msg_item_admin"><p>Type your concern or message to Customer Support</p></span>';

$("#concernType").on('click', 'li', function(){
        var concernType = $(this).html();
        $('.chat_converse').append("<span class='chat_msg_item chat_msg_item_user'><p>" + concernType + "</p></span>");

        if($(this).attr('id')){
            switch ($(this).attr('id')){
                case 'concernTypeCol':
                    $('.chat_converse').append("<span id='custTypelabel' class='chat_msg_item chat_msg_item_admin'><p>Are you a new or existing customer?</p></span><span id='custType' class='custTypelist chat_msg_item'><ul id='custType'><li id='newCustType'>New Customer</li><li id='existingCustType'>Existing Customer</li></ul></span>");
                    $("#custType").on('click', 'li', function(){
                        var custType = $(this).html();
                        $('.chat_converse').append("<span class='chat_msg_item chat_msg_item_user'>" + custType + "</span>");

                        if($(this).attr('id') == 'newCustType'){
                            $('.chat_converse').append(custName);
                            chatUserSend();
                        } else if ($(this).attr('id') == 'existingCustType'){
                            $('.chat_converse').append(custLoanAccountNum);
                            chatUserSend();
                            $('.chat_converse').append(custName);
                        }
                    });
                break;
            }
        }
    });

function chatUserSend(){
   $('#fab_send').click(function(event){
    var thought = $('#chatSend').val();
    $('.chat_converse').append("<span class='chat_msg_item chat_msg_item_user'>" + thought + "</span>");
    //sendMessage();

    $('#chatSend').val('');

    return false;
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="chat_converse" class="chat_conversion chat_converse">
        <span id="concernTypelabel" class="chat_msg_item chat_msg_item_admin">
            <p>Hello, how can we help you? Please select one?</p></span>
            <span id="concernType" class="chat_msg_item ">
              
            <ul id="concernType">
                <li id="concernTypeCol">I need assistance in paying my loan. (Collections)</li>
                <li id="concernTypeCS">I want to talk to a customer service representative. (Customer Service)</li>
                <li id="concernTypeHR">I want to explore job opportunities. (Human Resource)</li>
            </ul>
          </span>
		  </div>

		  <div class="fab_field">
        <a id="fab_send" class="fab">Send</a>
        <textarea id="chatSend" name="chat_message" placeholder="Send a message" class="chat_field chat_message"></textarea>
		  </div>

Sample expected output

1 个答案:

答案 0 :(得分:0)

能否请您尝试此代码。让我知道您是否想要这个。

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <div id="chat_converse" class="chat_conversion chat_converse">
            <span id="concernTypelabel" class="chat_msg_item chat_msg_item_admin">
                <p>Hello, how can we help you? Please select one?</p></span
            >
            <span id="concernType" class="chat_msg_item ">
                <ul id="concernType">
                <li id="concernTypeCol">
                    I need assistance in paying my loan. (Collections)
                </li>
                <li id="concernTypeCS">
                    I want to talk to a customer service representative. (Customer Service)
                </li>
                <li id="concernTypeHR">
                    I want to explore job opportunities. (Human Resource)
                </li>
                </ul>
            </span>

            <div id="chat_nodes"></div>
            <div id="chat_loan_acc"></div>
            </div>

            <div class="fab_field" style="display:none;">
            <a id="fab_send" class="fab">Send</a>
            <textarea
                id="chatSend"
                name="chat_message"
                placeholder="Send a message"
                class="chat_field chat_message"
            ></textarea>
            </div>

            <script>
            var custLoanAccountNum =
                '<span id="custLoanAccountNum" class="chat_msg_item chat_msg_item_admin"><p>What is your Loan Account Number?</p></span>';
            var custName =
                '<span id="custConcern" class="chat_msg_item chat_msg_item_admin"><p>Type your concern or message to Customer Support</p></span>';

            $("#concernType").on("click", "li", function() {
                var concernType = $(this).html();
                $("#chat_nodes").html(
                "<span class='chat_msg_item chat_msg_item_user'><p>" +
                    concernType +
                    "</p></span>"
                );
                $("#chat_loan_acc").html("");
                if ($(this).attr("id")) {
                switch ($(this).attr("id")) {
                    case "concernTypeCol":
                    $("#chat_nodes").append(
                        "<span id='custTypelabel' class='chat_msg_item chat_msg_item_admin'><p>Are you a new or existing customer?</p></span><span id='custType-span' class='custTypelist chat_msg_item'><ul id='custType'><li id='newCustType'>New Customer</li><li id='existingCustType'>Existing Customer</li></ul></span>"
                    );
                    $(".fab_field").hide();
                    break;
                    default:
                    $(".fab_field").show();
                    break;
                }
                }
            });

            $(document).on("click", "#custType li", function() {
                var custType = $(this).html();
                $("#chat_loan_acc").html(
                "<span class='chat_msg_item chat_msg_item_user'>" + custType + "</span>"
                );
                $(".fab_field").show();
                if ($(this).attr("id") == "newCustType") {
                $("#chat_loan_acc").append(custName);
                //chatUserSend();
                } else if ($(this).attr("id") == "existingCustType") {
                $("#chat_loan_acc").append(custLoanAccountNum);
                //chatUserSend();
                $("#chat_loan_acc").append(custName);
                }
            });

            $("#fab_send").click(function(event) {
                chatUserSend();
            });

            function chatUserSend() {
                var thought = $("#chatSend").val();
                //sendMessage();

                $("#chatSend").val("");

                return false;
            }
            </script>