我遇到问题,因为当我选择Individual
运营商类型时,隐藏字段为空。
我希望Business
填写的内容与individual
相同
如果business
和<div class="form-group">
<div class="col-md-10">
@Html.DropDownListFor(m => m.CarrierType, ViewBag.CarrierTypeList as List<SelectListItem>, new { @class = "btn btn-primary btn-block dropdown-toggle", id = "CarrierType" })
</div>
</div>
<!--Individual-->
<div id="Individual">
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.DriverLicenseNumber, new { @class = "form-control ", placeholder = "Driver's License Number", required = "required", tabindex = 15 })
@Html.ValidationMessageFor(m => m.DriverLicenseNumber, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.VehicleRegNumber, new { @class = "form-control ", placeholder = "Vehicle Registration Number", required = "required", tabindex = 16 })
@Html.ValidationMessageFor(m => m.VehicleRegNumber, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.VehiclePlateNumber, new { @class = "form-control ", placeholder = "Vehicle Plate Number", required = "required", tabindex = 17 })
@Html.ValidationMessageFor(m => m.VehiclePlateNumber, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.VecInsNum, new { @class = "form-control ", placeholder = "Vehicle Isurance Number", required = "required", tabindex = 18 })
@Html.ValidationMessageFor(m => m.VecInsNum, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.DropDownListFor(m => m.FleetType, ViewBag.NCFleetTypeList as List<SelectListItem>, new { @class = "btn btn-primary dropdown-toggle" })
</div>
</div>
</div>
<!--Business-->
<div id="Business">
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.DriverLicenseNumber, new { @class = "form-control ", placeholder = "Driver's License Number", required = "required", tabindex = 15 })
@Html.ValidationMessageFor(m => m.DriverLicenseNumber, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.VehicleRegNumber, new { @class = "form-control ", placeholder = "Vehicle Registration Number", required = "required", tabindex = 16 })
@Html.ValidationMessageFor(m => m.VehicleRegNumber, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.VehiclePlateNumber, new { @class = "form-control ", placeholder = "Vehicle Plate Number", required = "required", tabindex = 17 })
@Html.ValidationMessageFor(m => m.VehiclePlateNumber, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.VecInsNum, new { @class = "form-control ", placeholder = "Vehicle Isurance Number", required = "required", tabindex = 18 })
@Html.ValidationMessageFor(m => m.VecInsNum, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
@Html.DropDownListFor(m => m.FleetType, ViewBag.CFleetTypeList as List<SelectListItem>, new { @class = "btn btn-primary dropdown-toggle" })
</div>
</div>
</div>
之间的m.somthing相同,则获取相同的值。
我的cshtml如下:
function hideOnLoadCarrierType() {
$('#Individual').hide();
$('#Business').hide();
}
$(document).ready(function () {
hideOnLoadCarrierType();
$('#CarrierType').change(function () {
var value = $(this).val();
if (value == '1') {
$('#Individual').show();
$('#Business').hide();
} else if (value == '2') {
$('#Individual').hide();
$('#Business').show();
} else {
hideOnLoadCarrierType();
}
});
});
以下JS的作品完美无缺:
required
但问题是;当我填写业务信息并尝试注册时(模型中的所有字段均为Individual
),由于read_ascii
字段未设置而给我错误。
答案 0 :(得分:1)
您可以尝试为表单编写事件处理程序,并在提交表单之前执行您需要的任何逻辑。在下面的代码段中,我在填写表单时填写了Individuals
中Business
对应的空白字段:
// Assuming the form has a submit button with id = "submit-btn"
$('#submit-btn').on('click', function() {
var form = $(this).closest('form');
var businessDiv = form.find('#Business');
// Assuming this is only required when Business is selected
if(businessDiv.is(':visible')) {
var individual = form.find('#Individual input:text');
var business = businessDiv.find('input:text');
for(var i = 0; i < individual.length; ++i) {
// Perform the required logic here.
// I'm just forcing all empty fields in individuals to
// their business counterpart
if(individual[i].value === "") {
individual[i].value = business[i].value;
}
}
}
});
<强>演示强>
这是代码的现场演示,其中粗略描绘了呈现的HTML应该是什么样子:
http://www.bootply.com/ZHVTKLJNj3