jQuery 根据同时在另一个字段中输入的内容自动填充字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18474499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:42:25  来源:igfitidea点击:

Auto populate field base on what was entered in another field simultaneously

javascriptjqueryformsinput

提问by netizen0911

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:

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

<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>

回答by JustinMichaels

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

我根据您的要求制作了一个js 小提琴

HTML

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

JavaScript

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

回答by steo

I made a general example :

我做了一个一般的例子:

HTML

HTML

<input type="text" class="first">
<input type="text" class="second">

javascript

javascript

$(".first").on('keyup',function(){
    $(".second").val($(this).val());
});

http://jsfiddle.net/fmdwv/1/

http://jsfiddle.net/fmdwv/1/

For your purpose :

为了您的目的:

$("#address").on('change', function(){
  $("#shipping-address-1").val($(this).val());
});

回答by Ofir Israel

The onchangeevent should be used in the <input>tags themselves and not in the <form>.

onchange事件应在使用<input>标签本身,而不是在<form>

回答by ajay gupta

Create simple html file with two input text box. The first input text box will take the input from the user about a date format and second input text box will display the current date on this format. After the completion of first step, implement date selection library to select a date and format it according to the user input given in first input text b

使用两个输入文本框创建简单的 html 文件。第一个输入文本框将接受用户关于日期格式的输入,第二个输入文本框将显示此格式的当前日期。完成第一步后,实现日期选择库以选择一个日期并根据第一个输入文本 b 中给出的用户输入对其进行格式化