网页中有三个输入框 address, city, zip,

feng8848 2018-02-22 07:17:23
网页中有三个输入框 address, city, zip

我想分别输入内容 5660 Mixville Rd, Sealy, 77474,点击按钮 生成一个链接,

https://www.abc.com/?address=5660+Mixville+Rd&city=Sealy&zip=77474

用javascript如何写呀,多谢大家。
...全文
678 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
enl0ve 2018-03-02
  • 打赏
  • 举报
回复
<table>
		<tr>
			<td>address</td>
			<td><input type="text" name="" id="add"></td>
		</tr>
		<tr>
			<td>city</td>
			<td><input type="text" name="" id="city"></td>
		</tr>
		<tr>
			<td>zip</td>
			<td><input type="text" name="" id="zip"></td>
		</tr>
		<tr>
			<td>生成的链接:</td>
			<td><input type="text" name="" id="contact"></td>
		</tr>
		<tr><td><input type="button" name="contact" value="contact" onclick="contact();"></td></tr>
	</table>
	<script type="text/javascript">
		function contact(){
		var add = document.getElementById("add").value;
		var adds = add.split(" ").join("+");
		var city = document.getElementById("city").value;
		var zip = document.getElementById("zip").value;
		var contact = document.getElementById("contact");

		var contacts = "http://www.abc.com/?address=" + adds + "&city=" + city + "&zip=" + zip;
		contact.value = contacts;
		}
		
	</script>
似梦飞花 2018-02-22
  • 打赏
  • 举报
回复

<input name="address" />
<input name="city" />
<input name="zip" />
<script>
    var url='https://www.abc.com/?';
    var o={
        address:'',
        city:'',
        zip:''
    };
    [].slice.call(document.querySelectorAll('input')).forEach(function(item){
        item.addEventListener('input',function(){
            var value=item.value.replace(/\s+/g,'+');
            var name=item.name;
            o[name]=value;
            var x=url;
            var params=Object.keys(o).map(function(item){
                return item+'='+o[item];
            });
            x+=params.join('&');
            console.log(x);
        },false)
    })
</script>
  • 打赏
  • 举报
回复
<input type="text" id="address" />
<input type="text" id="city" />
<input type="text" id="zip" /><input type="button" onclick="createURL()" value="生成url" />
<script>
function createURL() {
var url = 'https://www.abc.com/?address=' + encodeURIComponent(document.getElementById('address').value)
+ '&city=' + encodeURIComponent(document.getElementById('city').value)
+ '&zip=' + encodeURIComponent(document.getElementById('zip').value);
alert(url)
}
</script>

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧