我在主页上有一个表单,我试图将输入字段值传递给另一个仅使用客户端的页面。问题是,我想在运行函数之前将传递的值重新显示回另一个表单字段,但我似乎无法使用REGEX替换“+”字符(还希望保持间距)。知道如何在没有太多控制的情况下完成这项任务吗?
第一页表格:
<form id="dlrhpform" type="GET" action="/dealer-locator">
<div id="hp-form-input">
<div style="padding-left:20px;">
<p>Enter address or zip code:</p>
<input style="float:left;" type="text" id="hpaddress" name="hpaddress"/>
</div>
</div>
<a href="javascript:;" onmouseover="MM_swapImage('hpsubmitbtn','','/btn_go_on.png',1)" onmouseout="MM_swapImgRestore()" />
<input onClick="_gaq.push(['_setAccount', 'UA-XXXXXXX-1'],['_trackEvent','Dealer Locator','Input Parameter', document.getElementById('hpaddress').value]);" style="position:relative; z-index:9999;" type="image" id="hpsubmitbtn" src="/btn_go.png" border="0" width="20" height="21" alt="hpsubmitbtn"/>
</form>
第二页表格:
<form name="user-location" id="user-location" method="post" action="#">
<div id="form-input" name="form-input">
<label for="address">Enter Address or Zip Code:</label>
<input type="text" id="address" name="address" style="width:200px;"/>
</div>
<div id="submit-btn"><input name="fetch" type="image" src="/searchbutton.png" onclick="address.value=address.value.replace(/,/g,''); _gaq.push(['_setAccount', 'UA-XXXXXXX-1'],['_trackEvent','Dealer Locator','Input Parameter', document.getElementById('address').value]);"/></div>
</form>
第二页脚本:
//Return Home Page Address From URL
var urladdress;
if(document.URL.indexOf("hpaddress") >= 0)
{
function getParams()
{
var idx = document.URL.indexOf('?');
parent.params = new Array();
if (idx != -1)
{
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++)
{
nameVal = pairs[i].split('=');
parent.params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();
urladdress = unescape(params["hpaddress"]);
}
window.onload = function()
{
if(typeof urladdress !== 'undefined')
{
document.getElementById("address").value = urladdress;
}
};
答案 0 :(得分:0)
做了一些研究,我发现更换加号的难点在于使用正则表达式中的加号表示还有更多的正则表达式。加号在javascript中用作运算符。但是,可以将其转义并替换如下。
var myString = "this+that+this+that++andthis++";
myString = myString.replace(/\+/g, " ");
alert(myString);