Javascript 如何使用“复选框”将当前地址更新为与永久地址相同的地址
通常,在网页上创建表单时,我需要我们的客户填写一个字段,例如永久地址,以及选中“当前地址”复选框后的“当前地址”,然后自动填写“当前地址”。 我可以使用JavaScript将表单的数据从一个字段复制到另一个字段,而不是让我们的客户填写两次表单Javascript 如何使用“复选框”将当前地址更新为与永久地址相同的地址,javascript,jquery,razor,asp.net-mvc-5,Javascript,Jquery,Razor,Asp.net Mvc 5,通常,在网页上创建表单时,我需要我们的客户填写一个字段,例如永久地址,以及选中“当前地址”复选框后的“当前地址”,然后自动填写“当前地址”。 我可以使用JavaScript将表单的数据从一个字段复制到另一个字段,而不是让我们的客户填写两次表单 @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div> <div class="well"> <b>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div>
<div class="well">
<b>Parmanent Address </b>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="col-lg-12">
@Html.LabelFor(model => model.ParFirstAddr, htmlAttributes: new { @class = "control-label " })
@Html.EditorFor(model => model.ParFirstAddr, new { htmlAttributes = new {@class = "form-control", autocomplete = "off", @placeholder = "Enter Address" } })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="col-md-12">
@Html.LabelFor(model => model.ParFirstPO, htmlAttributes: new { @class = "control-label " })
@Html.EditorFor(model => model.ParFirstPO, new { htmlAttributes = new { @class = "form-control", autocomplete = "off", @placeholder = "Enter P.O." } })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="col-md-12">
@Html.LabelFor(model => model.ParFirstPS, htmlAttributes: new { @class = "control-label " })
@Html.EditorFor(model => model.ParFirstPS, new { htmlAttributes = new { @class = "form-control", autocomplete = "off", @placeholder = "Enter P.S." } })
</div>
</div>
</div>
</div>
<br />
<div class="well">
<b>Present Address </b>
</div>
<input type="checkbox" name="filladdress" onclick="fillAddress()" /> Present address same as parmanent address.<br />
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="col-lg-12">
@Html.LabelFor(model => model.PreFirstAddr, htmlAttributes: new { @class = "control-label " })
@Html.EditorFor(model => model.PreFirstAddr, new { htmlAttributes = new {@id= "PreFirstAddr", @class = "form-control", autocomplete = "off", @placeholder = "Enter Address", style = "width: 5000px" } })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="col-md-12">
@Html.LabelFor(model => model.PreFirstPO, htmlAttributes: new { @class = "control-label " })
@Html.EditorFor(model => model.PreFirstPO, new { htmlAttributes = new { @class = "form-control", autocomplete = "off", @placeholder = "Enter P.O." } })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="col-md-12">
@Html.LabelFor(model => model.PreFirstPS, htmlAttributes: new { @class = "control-label " })
@Html.EditorFor(model => model.PreFirstPS, new { htmlAttributes = new { @class = "form-control", autocomplete = "off", @placeholder = "Enter P.S." } })
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-5 col-md-12">
<input type="submit" value="Submit" class="btn btn-success" />
</div>
</div>
</div>
}
函数填充地址
{
如果filladdress.checked==true
{
var addr=document.getElementByIdParFirstAddr.value;
var addrpo=document.getElementByIdParFirstPO.value;
var addrps=document.getElementByIdParFirstPS.value;
var copyaddr=addr;
var copyaddrpo=addrpo;
var copyaddrps=addrps;
document.getElementByIdPreFirstAddr.value=copyaddr;
document.getElementByIdPreFirstPO.value=copyaddrpo;
document.getElementByIdPreFirstPS.value=copyaddrps;
}
如果filladdress.checked==false,则为else
{
document.getElementByIdPreFirstAddr.value=;
document.getElementByIdPreFirstPO.value=;
document.getElementByIdPreFirstPS.value=;
}
}
你可以试试这个
function fillAddress(){
if ($(this).attr('checked')) {
$("#PreFirstAddr").val($("#ParFirstAddr").val());
$("#PreFirstPO").val($("#ParFirstPO").val());
$("#PreFirstPS").val($("#ParFirstPS").val());
}
else {
$("#PreFirstAddr").val('');
$("#PreFirstPO").val('');
$("#PreFirstPS").val('');
}
}
它不起作用,那么你可以使用setTimeoutfunction{},100你可以试试这个
function fillAddress(){
if ($(this).attr('checked')) {
$("#PreFirstAddr").val($("#ParFirstAddr").val());
$("#PreFirstPO").val($("#ParFirstPO").val());
$("#PreFirstPS").val($("#ParFirstPS").val());
}
else {
$("#PreFirstAddr").val('');
$("#PreFirstPO").val('');
$("#PreFirstPS").val('');
}
}
它不起作用,那么您可以使用setTimeoutfunction{},100将id添加到您的复选框中:
<input type="checkbox" id="filladdress" name="filladdress"/>
不要忘记在视图中包含jQuery库。在复选框中添加一个id:
<input type="checkbox" id="filladdress" name="filladdress"/>
不要忘记在视图中包含jQuery库。这里有什么问题?这里有什么问题?谢谢。这个答案非常有用。谢谢。这个答案非常有用。