2017-06-06 50 views
0

我有一個chatbot我創建,要求用戶的用戶名,然後將其作爲變量存儲。但是,在我的一個對話框樹中,我不得不要求用戶輸入另一個答案,我已使其工作,以便用戶可以通過按鈕點擊進行導航。模擬chatbot切換輸入法?

關於用戶名捕獲,「輸入欄」將從控件中刪除,並附加帶有每個郵件功能的按鈕。我試圖做到這一點(刪除按鈕,並添加一個輸入)的反作用,但我無法讓我的輸入做任何事情。它不會在輸入時提交,它不會保存一個變量,在我的試驗和錯誤中,我發現如果我嘗試設置一個關閉該味精值的變量,它會更新用戶名。

這裏是我的功能,以獲取名稱

function get_username() { 
    send_msg("Hello I'm Mumbles, the Help Bot", function(){ 
    send_msg("Who am I speaking with today?") 
    }); 
} 

function ai(msg) { 
    if (username.length < 3) { 
    username = msg; 
      send_msg("Nice to meet you"+username+". What are you working on today?" , function() { 
      $("#controls").append(
       '<button class="options one" value="my_certifications" type="button">My Certifications</button>' + 
       '<button class="options one" value="videos"type="button">Videos</butto/>' + 
       '<button class="options one" value="login"type="button">Login</butto/>' + 
       '<button class="options one" value="other"type="button">Other</butto/>' 
      ); 
      }); 
     } 
     // Remove text bar 
     $("#controls").empty(); 

}; 

這裏是我的功能,試圖捕捉電子郵件

} else if (b_val == "my_practicum") { 
     send_msg("What is the email address you submitted your practicum with? By having this we can give you a status update!" , function() { 
      $("#controls").append(
         '<textarea id="message" class="practicum-bar" placeholder="Type Your Name - Then Hit Enter"></textarea><button style="display:none;" id="sendMsg">Send</button>'     ); 
       email = msg; 
     console.log(email); 
     }); 
    } 
} 

如果你想看到這個動作,在這裏我JSFiddle您可以通過這個用戶路徑

輸入名稱>證書>我的實習會看到它>輸入電子郵件

任何幫助表示讚賞!謝謝!

+0

輸入電子郵件後你想要發生什麼 –

+0

@jackblank當它運行api函數時,它會詢問後續問題(尚未放入示例中)。我只需要能夠將其存儲爲變量 –

回答

0

看起來您通過執行$("#controls").empty();刪除controls,並且您想要將事件添加到動態創建的元素,因此您必須將該上下文添加到.on的第二個參數。現在看看you can see the console.log in the fiddle第二次點擊輸入,因爲jquery現在可以檢測到該項目。

$("#controls").on("keypress", "#message", function(event){ 

if (event.which == 13) { 
     console.log("my test"); 
     if ($("#slideThree").prop("checked")) { 
     $("#sendMsg").click(); 
     event.preventDefault(); 
     //*edit you can now change variables outside of scope* 
     } 
    } 
}); 

現在你可以做你的事件處理。