根据同时在另一个字段中输入的内容自动填充字段 [英] Auto populate field base on what was entered in another field simultaneously

查看:107
本文介绍了根据同时在另一个字段中输入的内容自动填充字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何根据使用javascript在另一个输入字段中输入的内容自动填充输入值.这是我的代码:

I was trying to figure out how to auto-populate the input value base on what was entered in another input field using javascript. Here's my code:

<script>
add = document.getElementById('address').value;
city = document.getElementById('city').value;
state = document.getElementById('state').value;
zip = document.getElementById('zip').value;
compAdd = add+" "+city+" "+state+" "+zip;
function showAdd(){
    document.getElementById('shipadd1').value=compAdd;
    document.getElementById('add1').innerHTML=compAdd;
}
</script>

<form action="" onchange="showAdd();">

Home Address: <input id="address" name="address" type="text"  /><br/>
Apt or Suite Number: <input id="suite" name="suite" type="text" /><br/>
City/Town: <input id="city" name="city" type="text" /><br/>
State:<select id="state" name="state" value="">...</select><br/>
Zip:<input id="zip" name="zip" type="text" value=""/><br/>


<p> Select Shipping Address Below: </p>

<input type="checkbox" id="shipping-address-1" name="address1" value=""><span id="shiadd1">(Print auto-populated shipping address here...)</span>

<input type="checkbox" id="shipping-address-2" name="address2" value="">ABC Corp 123 Main Street, My City, State Zip
</form>

推荐答案

我根据什么做了 js小提琴您要的.

I made a js fiddle based on what you're asking for.

HTML

Home Address:
<input id="address" name="address" type="text" />
<br/>Apt or Suite Number:
<input id="suite" name="suite" type="text" />
<br/>City/Town:
<input id="city" name="city" type="text" />
<br/>State:
<select id="state" name="state" value="">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
</select>
<br/>Zip:
<input id="zip" name="zip" type="text" value="" />
<br/>
<p>Select Shipping Address Below:</p>
<input type="checkbox" id="shipping-address-1" name="address1" value="">
<label for="shipping-address-1" id="shiadd1">(Print auto-populated shipping address here...)</label>
<input type="checkbox" id="shipping-address-2" name="address2" value="">
<label for="shipping-address-2">ABC Corp 123 Main Street, My City, State Zip</label>

JavaScript

$("#address,#suite,#city,#state,#zip").change(function () {
    var addressArray = [$("#address").val(), $("#suite").val(), $("#city").val(), $("#state").val(), $("#zip").val()];
    $("#shiadd1").text(addressArray.join(' '));
});

这篇关于根据同时在另一个字段中输入的内容自动填充字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆