我有一个网页,它显示了一个包含可变字段和标签的部分。我正在修改此页面,添加另一个单选按钮问题。
我希望字段标签根据选择的单选按钮而更改,而不必复制整个部分。
代码片段为:
Template template = new Template();
template.Nvc.Add("[!YesRadioButton]", Html.RadioButton("TransferISA_RadioButton", "Yes", ViewData["TransferISA_Yes"].Equals(true), new { id = "TranYes", onclick = "CheckRadioButton(this,'TransferIsa_Cell','No');", title = "To select 'Yes', 'No' has to be selected for a Child Trust Fund transfer in." }).ToHtmlString());
template.Nvc.Add("[!NoRadioButton]", Html.RadioButton("TransferISA_RadioButton", "No", ViewData["TransferISA_No"].Equals(true), new { id = "TranNo", onclick = "CheckRadioButton(this,'TransferIsa_Cell','No');" }).ToHtmlString());
template.Nvc.Add("[!TranErrorMsg]", (Html.ValidationMessage("TransferISA_RadioButton") != null) ? Html.ValidationMessage("TransferISA_RadioButton").ToHtmlString() : "");
template.HtmlTemplate = cmsText.Text;
template.Nvc.Add("[!CTFYesRadioButton]", Html.RadioButton("CTF_RadioButton", "Yes", ViewData["CTF_Yes"].Equals(true), new { id = "CTFYes", onclick = "CheckRadioButton(this,'TransferIsa_Cell','No');", title = "To select 'Yes', 'No' has to be selected for a Junior ISA transfer in." }).ToHtmlString());
template.Nvc.Add("[!CTFNoRadioButton]", Html.RadioButton("CTF_RadioButton", "No", ViewData["CTF_No"].Equals(true), new { id = "CTFNo", onclick = "CheckRadioButton(this,'TransferIsa_Cell','No');", }).ToHtmlString());
template.Nvc.Add("[!CTFErrorMsg]", (Html.ValidationMessage("CTF_RadioButton") != null) ? Html.ValidationMessage("CTF_RadioButton").ToHtmlString() : "");
var label1 = ***SOMETHING GOES HERE*** ? "name1" : "name2"; %>
<script language="javascript" type="text/javascript" src="<%=ResolveUrl("~/Resources/Scripts/Application.js")%>"></script>
<%using (Html.BeginForm(null, null, FormMethod.Post, new { role = "form", @class = "form-horizontal" }))
{ %>
<h1><%=cmsText.Title %></h1>
<%=template.ToString() %>
<div id="TransferIsa_Cell" style="display:<%=ViewData["ISADisplay"]%>;">
<div class="row">
<div class="col-sm-12 col-md-7">
<h3>Current ISA manager details</h3>
<div class="form-group">
<label class="col-sm-4 col-md-6 control-label" for="CurrentManager_TextBox"><%= label1 %></label>
模板部分用于我的CMS占位符。我需要想出一种方法,使标签1成为一个变量,具体取决于"YesRadiobButton"one_answers"CTFYesRadiobButton"的选择。
有人知道怎么做吗?
我尝试了以下操作,但没有成功:
var label1 = ViewData["TransferISA_Yes"].Equals(true) ? "name1" : "name2";
var label1 = ViewData["TransferISA_Yes"] == "true" ? "name1" : "name2";
如果我能帮上忙的话,我真的不想使用Java/Jquery,但如果必须的话我会的。
EDIT-此答案仅适用于单选按钮位于同一页面的情况
如果要从中获取数据的元素与标签在同一页上,则不应该真正使用ViewData。。这更多的是为了将一些东西从控制器传递到视图
jQuery是你最好的选择,我认为
$('input:radio[name=nameofradiobutton]').on('change', function()
{
var selection = $(this).val();
if(selection == something)
{
$("label[for='CurrentManager_TextBox']").text("some text");
}
});
感谢@krilovich的回复,但当权者没有决定只有一组字段标签,所以这个问题不再有效。