2016-11-10 47 views
0

所以我試圖在這裏傳遞「x」的值,它將生成隨機值。將變量傳遞給mailto主題

<script> 

    var result = Math.random().toString(36).substring(2,5); 
    window.onload = function() { 
    document.getElementById("x").innerHTML=result; 
    } 

</script> 

我想將值傳遞給我的html代碼,在mailto主題。所以當人們點擊鏈接,它會自動打開默認的郵件和發送電子郵件的主題爲「特價促銷+(隨機碼)」,以[email protected]

<p style="color: #5b5b5b; text-align: left; id: x;"> 
<a title="Book Now" href="mailto:[email protected]?subject=Special%20Promotion%20"+x>Book Directly with Us</a></p> 

我試圖在調用值編號並在主題中放入「x」,但正如我所料,它不起作用。你們能幫我嗎?我需要知道如何在主題電子郵件中顯示價值。

在此先感謝。

+0

難道你看看這個:http://stackoverflow.com/questions/4782068/can-i-set-subject-content-of-email-with-using-mailto – Sanj

回答

0

首先從您的HTML中刪除+ x即a標記的屬性。 然後加ID給你a標籤mailTo在這種情況下(如下圖所示)。

$(function() { 
 
    
 
var result = Math.random().toString(36).substring(2,5); 
 
var href = $("#mailTo").attr('href') 
 
var newHref = href + result 
 
$("#mailTo").attr("href", newHref) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a title="Book Now" id='mailTo' href="mailto:[email protected]?subject=Special%20Promotion%20">Book Directly with Us</a></p>

+0

不,它仍然不起作用。我剛剛看到你的代碼,它不會生成一個隨機代碼,它更像是你手動輸入代碼。所以代碼總是相同的,每次我點擊鏈接 –

+0

@AindindSantikaDevi隨機代碼我把它從你的問題,你可以修改它,因爲你想 –

+0

我把JavaScript上面的一個標記,但它仍然不工作 –