0

我在主页上有一个表单,并试图将输入字段值传递到另一个仅在客户端使用的页面。问题是,我想在运行函数之前将传递的值重新显示回另一个表单字段,但我似乎无法使用 REGEX 替换“+”字符(也想保持间距)。知道如何在没有太多控制的情况下完成此任务吗?

第 1 页表格:

            <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;
    }
};
4

1 回答 1

0

做了一些研究,我发现替换加号的难点在于,正则表达式中的加号用于表示还有更多的正则表达式要遵循。加号在 javascript 中用作运算符。但是,可以将其转义并替换如下。

var myString = "this+that+this+that++andthis++";
myString = myString.replace(/\+/g, " ");
alert(myString);
于 2013-01-15T16:46:30.027 回答