选择列表项目值不适用于JavaScript asp mvc
我有一个注册表单。并根据个人或企业角色出现两个不同的字段。选择列表项目值不适用于JavaScript asp mvc
隐藏&显示js工作正常。但我猜想强制在个人到企业中的所有空白字段不适用于FleetType
这是模型中的Integer
。但选择Individual
并填写它的所有表单!不只是工作Business
....
我有register.cshtml:
ViewBag.NCFleetTypeList = new List<SelectListItem> {
new SelectListItem { Text="Select Fleet Type", Value=null},
new SelectListItem { Text="Car", Value="1"},
new SelectListItem { Text="Cargo Van", Value="2"},
new SelectListItem { Text="Pickup Truck", Value="3"},
new SelectListItem { Text="10' Truck", Value="4"},
new SelectListItem { Text="15' Truck", Value="5"},
new SelectListItem { Text="17' Truck", Value="6"},
new SelectListItem { Text="20' Truck", Value="7"},
new SelectListItem { Text="26' Truck", Value="8"}
};
ViewBag.CFleetTypeList = new List<SelectListItem>
{
new SelectListItem { Text="Select Fleet Type", Value=null},
new SelectListItem { Text="10' Truck", Value="4"},
new SelectListItem { Text="15' Truck", Value="5"},
new SelectListItem { Text="17' Truck", Value="6"},
new SelectListItem { Text="20' Truck", Value="7"},
new SelectListItem { Text="26' Truck", Value="8"}
};
<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">
<h4>Individual Information</h4>
<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">
<h4>Business Information</h4>
<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>
JS我有以下几点:
// 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;
}
}
}
});
当我选择Individual
,一切工作完美的,我可以注册。
但是当我选择Business
,如果我从下拉列表中选择“20' 卡车”,我得到错误The value 'Select Fleet Type,7' is not valid for Fleet Type.
我能得到一些帮助?谢谢!!
回答:
有一件事可能会导致这个问题是,当您从运营商类型中选择Business
并继续提交表单时,jQuery代码片段将填写#Individual
中的所有空字段,但#Individual
中的下拉列表为Select Fleet Type
,并且这会在提交表单时导致错误到服务器,如果是这种情况,你可以尝试添加以下几行o F代码的片段,使#Individual
下的下拉被更新为#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;
}
}
// Add the following lines
// let the business dropdown update the individual dropdown
var individualFleetSelect = $(form.find('#Individual .dropdown-toggle'));
var businessFleetSelect = $(form.find('#Business .dropdown-toggle'));
individualFleetSelect.val(businessFleetSelect.val());
}
});
以上是 选择列表项目值不适用于JavaScript asp mvc 的全部内容, 来源链接: utcz.com/qa/257792.html