从两个输入中替换两个document.write值

时间:2011-10-27 22:16:18

标签: javascript url document src

下面的javascript会在输入条目中将http://www.google.co.uk/webhp?hl=en替换为http://link-ads.blogspot.com/?url=http%3A%2F%2Fwww.google.co.uk%2Fwebhp%3Fhl%3Den

<script type="text/javascript">
function showMe(e) {
paramencode = encodeURIComponent(e.value)
document.write('http://link-ads.blogspot.com/?url='+paramencode+'');
}
</script>
<input type="text" id="foo" placeholder="URL here" onchange="showMe(this)" />

它运行正常,但我现在需要包含从第二个输入继承到文档结尾的&name= +值,即。

document.write('http://link-ads.blogspot.com/?url='+paramencode+'&name='+andnameinput+'');

其中'+andnameinput+'将继承第二个<input>

中的值

EDITED 如果我将http://www.google.co.uk/webhp?hl=en插入第一个输入

<input type="text" id="foo" placeholder="URL here"/>

document.write的输出应为http://link-ads.blogspot.com/?url=http%3A%2F%2Fwww.google.co.uk%2Fwebhp%3Fhl%3Den

但它不完整,因为它应该有&amp; name = infront,用户输入值为我需要第二个输入

<input type="text" id="foo2" placeholder="Name here"/>

并说如果输入了值123456789,则document.write返回的输出应为http://link-ads.blogspot.com/?url=http%3A%2F%2Fwww.google.co.uk%2Fwebhp%3Fhl%3Den&name=123456789

1 个答案:

答案 0 :(得分:0)

如果我理解得很好,你想要这样的东西:

<script type="text/javascript">
    function showMe(e) {
        paramencode = encodeURIComponent(e.value)
        document.getElementById('urlEncoded').innerHTML = document.getElementById('urlEncoded').innerHTML + 'http://link-ads.blogspot.com/?url='+paramencode;
    }
    function UpdateParam() {
        document.getElementById('urlEncoded').innerHTML = document.getElementById('urlEncoded').innerHTML + '&name='+ document.getElementById('foo2').value;
    }
</script>
<div id="urlEncoded"></div>
<input type="text" id="foo" placeholder="URL here" onchange="showMe(this)" />
<input type="text" id="foo2" placeholder="SecondParam" onchange="UpdateParam()" />