2017-01-31 14 views
1

單擊電子郵件表單提交按鈕時需要按qg("identify", {"email": "<email address of the user"});。我需要從第三方解決方案中推入代碼,我只能使用html/css/javascript。在變量中獲取電子郵件然後按下事件onclick

在這裏,我插入jQuery UI的彈出形式

$.ajax({ 
    url: '//code.jquery.com/ui/1.12.1/jquery-ui.js', 
    dataType: 'script', 
    cache: true, 
    success: function() { 
    $("#dialog").dialog(); 
    } 
}); 

然後我插入的jquery UI CSS在頭

$("<link/>", { rel: "stylesheet", type: "text/css", href: "//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"}).appendTo("head"); 

然後我推彈出到主體

$("body").append('<div id="dialog" title="BEVAKA PRODUKT"><div id="dialog-form" title="Create new user"><p class="validateTips">Här kan du skriva in din e-post och få ett mail när produkten finns i lager igen</p><form><fieldset><label for="email">E-postadress</label><input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all"><input type="submit" tabindex="-1" style="position:absolute; top:-1000px"></fieldset></form></div><button id="create-user">Bevaka</button></div>'); 

她,我取的電子郵件地址在VAR emailTest

var emailTest = $("#email").val(); 

在這裏我提醒上點擊emailTest變量

$("#create-user").click(function(){ 
    alert(emailTest); 
}); 

這需要在電子郵件的形式提交要推什麼

//qg("identify", {"email": "<email address of the user"}); 

Protoype

var email = $("#email").val(); 

$("#create-user").click(function(){ 
    qg("identify", {"email": "email"}); 
}) 
+0

電子郵件值不會粘在變量 –

+0

嘗試在ajax成功中添加一些控制檯日誌,單擊處理程序。 javascript/jquery是基於事件的。所以,你需要在發生事件時檢查/獲取值,而不是使用變量名。 – nightgaunt

+0

@nightgaunt https://jsfiddle.net/hLcprteh/ –

回答

1

正如我前面提到的,使用Javascript/jQuery是基於事件的。

首先,

$.ajax({ 
    url: '//code.jquery.com/ui/1.12.1/jquery-ui.js', 
    dataType: 'script', 
    cache: true, 
    success: function() { 
    $("#dialog").dialog(); 
    } 
}); 

您需要確保#dialogue存在,你調用它的對話框之前。您已添加腳本以將#dialogue div直接附加到body,這會在當前腳本加載時觸發。

同樣,當你添加代碼

var email = $("#email").val(); 

變量將被分配當值電流負載腳本。在那個時候,你的電子郵件字段顯然是空的。

單擊按鈕時,您想獲得#email字段的值。因此,將該代碼移到點擊處理程序中。

+0

超級英雄是你是什麼 –

相關問題