0

我基本上是在尝试使用用户输入创建一个对 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

4 回答 4

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
}
于 2012-06-23T06:07:58.803 回答
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");
于 2012-06-23T06:23:06.357 回答
-1

您可能只想使用内置的 JavaScript 函数encodeURIencodeURIComponentescape视情况而定:http: //www.w3schools.com/jsref/jsref_encodeuri.asp

于 2012-06-23T06:00:52.360 回答
-1

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

于 2012-06-23T06:01:14.743 回答