2017-08-10 57 views
0

我希望人們可以分享推特基於人名輸入的名稱的獨特鏈接。我可以通過私人變量在JavaScript中的另一個功能

首先,我創建一個函數genurl,它可以根據用戶輸入他們的名字來生成url。

其次,我希望人們可以在Twitter上分享這些網址。

現在的問題是我得到一個獨特的網址基於每個人。我怎麼能通過shareTwitter函數url ($("#result").html(url))變量attr1

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta charset="UTF-8"> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 

 

 
<title>Generate Your Referral URL Link</title> 
 

 

 
<style> 
 
h2 { 
 
    margin: 5px 0; 
 
    padding:0; 
 
} 
 
td:first-child { 
 
\t width:150px; 
 
} 
 
td { 
 
\t width:300px; 
 
} 
 
input { 
 
\t width:100%; 
 
} 
 
</style> 
 

 
</head> 
 
<body> 
 

 
<!-- Genenrate a unique url interface --> 
 

 
<h1 style="margin:30px 0 0 50px;">Generate Your Referral URL Link</h1> 
 
<table style="margin:0 50px;"> 
 
<tr><td>Backer Name:</td><td><input type="text" name="name" id="name" class="name" placeholder="Your Kickstarter Display Name (Case Sensitive)"></td></tr> 
 
<tr><td>Join Time:</td><td><input type="text" name="time" id="time" class="time" placeholder="Month/Year You Joined Kickstarter"></td></tr> 
 
<tr><td>Reward You Pledged:</td> 
 
\t <td> 
 
<select name="reward" class="reward"> 
 
\t \t <option value=""></option> 
 
\t \t <option value="seb1">SUPER Early Bird: Charcoal/Snow</option> 
 
\t \t <option value="seb2">SUPER Early Bird: Oak</option> 
 
\t \t <option value="eb1">Early Bird: Charcoal/Snow</option> 
 
\t \t <option value="seb3">SUPER Early Bird: Onyx</option> 
 
\t \t <option value="seboo">Special Early Bird: Oak/Onyx</option> 
 
\t \t <option value="ks1">Kickstarter Special: Charcoal/Snow</option> 
 
\t \t <option value="seb4">Special Early Bird CONTINUED: Oak/Onyx</option> 
 
\t \t <option value="eb2">Early Bird: Oak</option> 
 
\t \t <option value="eb3">Early Bird: Onyx</option> 
 
\t \t <option value="ks2">Kickstarter Special: Oak</option> 
 
\t \t <option value="dr">Developer Reward</option> 
 
\t \t <option value="ks3">Kickstarter Special: Onyx</option> 
 
\t \t <option value="pc1">Personal Collection</option> 
 
\t \t <option value="pc2">Premium Collection</option> 
 
\t \t <option value="a4">Any 4</option> 
 
\t \t <option value="a5">Any 5</option> 
 
</select></td></tr> 
 
<tr><td></td></tr> 
 
</table> 
 
<div style="margin:0 50px;"> 
 
<button type="button" id='genurl' onclick="genurl()">Create</button> 
 
</div> 
 
<div id="result" style="margin: 0 30px;"></div> 
 
<div class="msg" style="margin: 0 30px;"></div> 
 
</div> 
 

 
<div style="margin-top: 60px;"> 
 

 

 
\t 
 
<!-- share twitter with the unique url interface--> 
 

 
<a href="javascript:void(0)" id="twitterShare" onclick="shareTwitter()" target="_blank"> 
 
\t <img src="https://daks2k3a4ib2z.cloudfront.net/58bad0d628e47e3e2911d6aa/58bad0d728e47e3e2911d711_fa-twitter_100_0_02779e_none.png" /> 
 
\t <br /><p>Share on Twitter</p> 
 
</a> 
 
</div> 
 
<script type="text/javascript"> 
 

 
//Generate unique url 
 
function genurl(){ 
 
var name = $(".name").val(); 
 
\t 
 
\t name = name.trim(); 
 
\t var time = $(".time").val(); 
 
\t time = time.trim(); 
 
\t var reward = $(".reward").val(); 
 

 
    \t if(name.length > 0 && time.length > 0 && reward.length > 0) { 
 

 
\t \t var url = "https://www.kickstarter.com/projects/mobvoi/ticwatch-2-the-most-interactive-smartwatch?utm_campaign=kickstarter_backer&utm_medium=referral&utm_source=social_share&backer_id="; 
 
\t \t url = url + encodeURIComponent(name + "_" + time + "_" + reward); 
 
\t \t $("#result").html(url); \t 
 

 
\t } else { 
 
\t \t $(".msg").html('<h2>please write Backer Name, Join Time and Reward You Pledged!</h2>'); 
 
\t } 
 

 
}; 
 

 

 
//share twitter with the unique url 
 

 
function shareTwitter() { 
 
\t 
 
\t // How could i pass the $("#result").html(url) to variable attr1? 
 
\t var attr1 = genurl(); 
 

 

 
\t var attr = "https://twitter.com/home?status=" + attr1; 
 
    document.getElementById("twitterShare").setAttribute('href', attr); 
 

 
} 
 

 
</script> 
 

 
</body> 
 
</html>

+0

''中返回genurl url'()'.... –

回答

0

genurl返回值。

function genurl(){ 

    var name = $(".name").val(); 

    name = name.trim(); 
    var time = $(".time").val(); 
    time = time.trim(); 
    var reward = $(".reward").val(); 

    if(name.length > 0 && time.length > 0 && reward.length > 0) 
    { 
     var url = "https://www.kickstarter.com/projects/mobvoi/ticwatch-2-the-most-interactive-smartwatch?utm_campaign=kickstarter_backer&utm_medium=referral&utm_source=social_share&backer_id="; 
     url = url + encodeURIComponent(name + "_" + time + "_" + reward); 
     return url; 
    } 
    else{ 
     $(".msg").html('<h2>please write Backer Name, Join Time and Reward You Pledged!</h2>'); 
     return null; 
    } 
} 

然後在你的shareTwitter函數使用這個值:

function shareTwitter() { 

    // How could i pass the $("#result").html(url) to variable attr1? 
    var attr1 = genurl();  
    if(attr1){ // check value was returned! 
     var attr = "https://twitter.com/home?status=" + attr1; 
     document.getElementById("twitterShare").setAttribute('href', attr); 
    } 

} 
相關問題