我有一个包含两个文本框,两个下拉列表和一个标签的表单。当我填写前两个文本框并选择两个下拉列表的值时,所有值将被串联起来并放入标签中。因此,现在单击“保存”按钮时,我只想发布标签的值,而不是其他文本框和清单。如何激发标签的post方法,并禁用其他方法?下面是我到目前为止的代码:
<div class="panel-body">
@using (Html.BeginForm())
{
<div class="form-group">
<div class="row">
<div class="col-md-6">
@Html.Label("Part/Location", new {@class = "control-label"})
@Html.TextBox("PartLocation", null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="col-md-6">
@Html.Label("Index", new {@class = "control-label"})
@Html.TextBox("Index", null , new {@class = "form-control"})
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="row">
<div class="col-md-6">
@Html.Label("Measurement", new {@class = "control-label"})
@Html.DropDownList("Measurements", "Select measurement")
@Html.ValidationMessageFor(model => model.ChannelGroupId)
</div>
<div class="col-md-6">
@Html.Label("Location", new {@class = "control-label"})
@Html.DropDownList("DirectionTypes","Select direction")
@Html.ValidationMessageFor(model => model.ChannelGroupId)
</div>
</div>
<div class="row">
<div class="col-md-6">
@Html.LabelFor(model => model.ChannelGroupId, new {@class = "control-label"})
@Html.DropDownListFor(x => x.ChannelGroupId, Model.ChannelGroups, "Select Channel Group", new {@class = "form-control"})
@Html.ValidationMessageFor(model => model.ChannelGroupId)
</div>
<div class="col-md-6">
<label class="control-label"></label>
<a href="#" id="addChannelGroup" class="form-control" style="border: none">
<i class="fa fa-plus-circle">Add Group</i>
</a>
</div>
</div>
<br />
<div class="row">
<div class="col-md-6">
@Html.Label("Channel name: ", new { id = "channelName",@class = "control-label" })
</div>
</div>
</div>
这是Jquery处理串联的方法:
$("#PartLocation, #Index,#Measurements,#DirectionTypes").change(function () {
var partLocationText = $("#PartLocation").val();
var indexText = $("#Index").val();
var mesurementSelected = $("#Measurements option:selected").text();
var directionSelected = $("#DirectionTypes option:selected").text();
$("#channelName").empty();
$("#channelName").append("Channel name: " + partLocationText + "_" + indexText);
if (mesurementSelected != "Select measurement") {
$("#channelName").append("_" + mesurementSelected);
}
if (directionSelected != "Select direction") {
$("#channelName").append("_" + directionSelected);
}
});
您可以在此处完成多项操作,以实现所需的目标。
1.根据HTML规范,如果所有HTML控件(输入,选择)没有名称属性,则它们不会发布。因此,简单地从这些控件中删除名称属性将阻止它们发布其值。除此之外,由于标签值未回发,因此您还必须创建一个隐藏的文本框,将其值更新为串联的值(即标签的值),然后再进行发布。
$(function(){
$("#PartLocation, #Index,#Measurements,#DirectionTypes").removeAttr('name');
//this will remove their name attribute and hence their values wont be posted back
})
并更新隐藏文本框的值(input [type =“ text”])。
$("#PartLocation, #Index,#Measurements,#DirectionTypes").change(function () {
//your code
//let cs be the final concatenated string.
$("#channelName").append(cs);
$('#hiddenInput').val(cs);
});
2.您可以只使用Javascript来执行发布机制,并且仅发布要发布的值。显然,要做到这一点,您最好将Action Method的返回类型更改为HttpResult
OR,JsonResult
以更适合您的方式为准。
$(function(){
$('form').submit(function(f, e){
e.preventDefault();
//callculate your concatenated string.
//assumed to be cs
$.ajax(url/*url to ur post action method*/, {
//fill in the suitable params see the jquery link for more help
})
})
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句