使用javascript填充输入值

时间:2014-08-17 16:34:41

标签: javascript jquery

我需要一个JavaScript解决方案。我在用户订阅后从成功页面中收集了来自aweber的数据。所以url喜欢:

http://example.com/success/?email=example@email.com&name=Jhon&custom_phone=0005551114&custom_location=USA

他们提供了一些javascript解决方案来在网页或成功页面中显示数据......

Referance:https://help.aweber.com/entries/21696333-How-Do-I-Display-Subscribers-Names-or-Email-Addresses-On-My-Thank-You-Page-

所以当我把

<script type="text/javascript">formData.display("name")</script>
<script type="text/javascript">formData.display("email")</script>
<script type="text/javascript">formData.display("custom_phone")</script>
<script type="text/javascript">formData.display("custom_location")</script>

值将显示在我的页面中。但是如何将值放在成功页面的输入字段中?

<form action="" method="post">
<input type="text" name="username" value="Jhon">
<input type="text" name="useremail" value="example@email.com">
<input type="text" name="phoneno" value="0005551114">
<input type="text" name="country" value="USA">
<input tyoe="submit" value="Submit">
</form>

请帮助..

1 个答案:

答案 0 :(得分:1)

您可以稍微更改Javascript代码,如下所示:

<script type="text/javascript">
 var formData = function() {  var query_string = (location.search) ? ((location.search.indexOf('#') != -1) ? location.search.substring(1, location.search.indexOf('#')) : location.search.substring(1)) : '';  
var elements = [];  
if(query_string) {     
    var pairs = query_string.split("&");     
    for(i in pairs) {     
        if (typeof pairs[i] == 'string') {           
            var tmp = pairs[i].split("=");           
            var queryKey = unescape(tmp[0]);           
            queryKey = (queryKey.charAt(0) == 'c') ? queryKey.replace(/\s/g, "_") : queryKey;   
            elements[queryKey] = unescape(tmp[1]);      
             }    
       } 
 }  
return {     
    display: function(key) {         
        if(elements[key]) {           
             document.write('<input type="text" name="' + key + '" value ="' + elements[key] + '"');         
         } 
         else {         
               document.write("<!--If desired, replace everything between these quotes with a default in case there is no data in the query string.-->");          
          }     
  }   
} 
}
(); </script>

您的HTML将如下所示:

<form action="" method="post">
<script type="text/javascript">formData.display("name")</script>
<script type="text/javascript">formData.display("email")</script>
<script type="text/javascript">formData.display("custom_phone")</script>
<script type="text/javascript">formData.display("custom_location")</script>
<input tyoe="submit" value="Submit">
</form>