返回“undefined”的简单JavaScript字符串变量

时间:2012-06-23 05:56:47

标签: javascript

我基本上是尝试使用用户输入创建一个友好的url查询字符串。变量替换空格和@s并将它们全部加在一起,然后函数应显示段落中的url。这让我感到非常愚蠢,因为我知道这很简单,我搞砸了。请指出我的错误。作为旁注,是否有更好的方法可以在没有所有变量的情况下进行所有这些替换?

以下代码应返回类似于的字符串: 的 https://www.somewebsite.com/formhandler.php?&fname=gordie&address=123+main+street&email=something%40somewhere.com

<SCRIPT TYPE="TEXT/JAVASCRIPT">

var fnstr = document.getElementById("firstname").value; 
var fncutspace = fnstr.trim();
var fnrepspace = fncutspace.replace(/ /g,"+");
var fnfinal = fnrepspace.replace("@","%40");
var adstr = document.getElementById("address").value; 
var adcutspace = adstr.trim();
var adrepspace = adcutspace.replace(/ /g,"+");
var adfinal = adrepspace.replace("@","%40");
var emstr = emdocument.getElementById("firstname").value; 
var emcutspace = emstr.trim();
var emrepspace = emcutspace.replace(/ /g,"+");
var emfinal = emrepspace.replace("@","%40");
var domurl = "https://www.somewebsite.com/formhandler.php?&fname=";
var secondchunk = "&address=";
var thirdchunk = "&email=";
var url = domurl + fnfinal + secondchunk + adfinal + thirdchunk + emfinal;

function createurl() {
document.getElementById("demo").innerHTML = url;
}
</SCRIPT>

<FORM action="javascript:createurl()">
Fisrt Name:<INPUT id="firstname" TYPE="TEXT"><br>
street address:<INPUT id="address" TYPE="TEXT"><br>
email address:<INPUT id="email" TYPE="TEXT"><br>
<input type="submit" value="GO">
</FORM>

<p id="demo"></p>

4 个答案:

答案 0 :(得分:1)

<FORM action="javascript:createurl()">
Fisrt Name:<INPUT id="firstname" TYPE="TEXT"><br>
street address:<INPUT id="address" TYPE="TEXT"><br>
email address:<INPUT id="email" TYPE="TEXT"><br>
<input type="submit" value="GO">
</FORM>

<p id="demo"></p>

<SCRIPT TYPE="TEXT/JAVASCRIPT">

var fnstr = document.getElementById("firstname").value; 
var fncutspace = fnstr.trim();
var fnrepspace = fncutspace.replace(/ /g,"+");
var fnfinal = fnrepspace.replace("@","%40");
var adstr = document.getElementById("address").value; 
var adcutspace = adstr.trim();
var adrepspace = adcutspace.replace(/ /g,"+");
var adfinal = adrepspace.replace("@","%40");
var emstr = emdocument.getElementById("firstname").value; 
var emcutspace = emstr.trim();
var emrepspace = emcutspace.replace(/ /g,"+");
var emfinal = emrepspace.replace("@","%40");
var domurl = "https://www.somewebsite.com/formhandler.php?&fname=";
var secondchunk = "&address=";
var thirdchunk = "&email=";
var url = domurl + fnfinal + secondchunk + adfinal + thirdchunk + emfinal;

function createurl() {
document.getElementById("demo").innerHTML = url;
}
</SCRIPT>

您应该在window.onload回调函数中执行所有依赖于DOM的javascript函数。

window.onload = function ()
{
Javascript code goes here
}

答案 1 :(得分:1)

如果它应该基于用户输入,你应该移动要声明的变量并在createurl内设置:

function createurl() {
    var fnstr = document.getElementById("firstname").value; 
    var fncutspace = fnstr.trim();
    var fnrepspace = fncutspace.replace(/ /g,"+");
    var fnfinal = fnrepspace.replace("@","%40");
    // etc.
    var url = domurl + fnfinal + secondchunk + adfinal + thirdchunk + emfinal;

    document.getElementById("demo").innerHTML = url;
}

这样,它们将在调用函数时设置为当前输入值。

  

作为旁注,是否有更好的方法可以在没有所有变量的情况下进行所有这些替换?

您可以链接方法调用:

var fnInput = document.getElementById('firstname');
var fnfinal = fnInput.value.trim().replace(/ /g,"+").replace("@","%40");

答案 2 :(得分:-1)

您可能只想酌情使用内置的JavaScript函数encodeURIencodeURIComponentescapehttp://www.w3schools.com/jsref/jsref_encodeuri.asp

答案 3 :(得分:-1)

你应该使用encodeURIComponent(string)而不是修剪/替换你现在正在做的事。