当我在表单字段中输入时,底部的输入也应该自动写入与上述字段相同的地址。
当我在下面看到输入时,我正在使用的 JavaScript 正在工作,但我需要在表单中提交它。我在控制台中得到的错误是“TypeError: autoAddress is not a function”下面的表格 WORKS
<form id="addNews" name="address" action="addevent.php" method="post" class="addStuff" enctype="multipart/form-data">
<h3>Address1:</h3><input type="text" class="title" name="address1" onkeyup="autoAddress();">
<h3>Address2:</h3><input type="text" class="title" name="address2" onkeyup="autoAddress();">
<h3>City:</h3><input type="text" class="title" name="city" onkeyup="autoAddress();">
<h3>County:</h3><input type="text" class="title" name="county" onkeyup="autoAddress();">
<h3>PostCode:</h3><input type="text" class="title" name="pcode" onkeyup="autoAddress();">
</form>
<input type="text" name="fulladdress" class="title" id="autoAddress" />
我需要的东西不起作用,如下所示。我确定我只是错过了一些简单的东西。请注意,底部的输入位于表单内部。
<form id="addNews" name="address" action="addevent.php" method="post" class="addStuff" enctype="multipart/form-data">
<h3>Address1:</h3><input type="text" class="title" name="address1" onkeyup="autoAddress();">
<h3>Address2:</h3><input type="text" class="title" name="address2" onkeyup="autoAddress();">
<h3>City:</h3><input type="text" class="title" name="city" onkeyup="autoAddress();">
<h3>County:</h3><input type="text" class="title" name="county" onkeyup="autoAddress();">
<h3>PostCode:</h3><input type="text" class="title" name="pcode" onkeyup="autoAddress();">
<input type="text" name="fulladdress" class="title" id="autoAddress" />
</form>
JavaScript如下
function autoAddress(){
var address1 = document.address.address1.value;
var address2 = document.address.address2.value;
var city = document.address.city.value;
var county = document.address.county.value;
var postcode = document.address.pcode.value;
var parts = [
address1,
address2,
city,
county,
postcode
];
var address = new Array();
for (var i=0; i<=parts.length; i++){
if (parts[i]){
address.push(parts[i]) ;
}
}
var joined = address.join(', ');
document.getElementById('autoAddress').value = joined;
}