变量标签取决于选择哪个单选按钮

时间:2015-02-26 15:32:37

标签: javascript c# jquery html

我有一个网页,显示包含可变字段和标签的部分。我正在修改此页面以添加另一个单选按钮问题。

我希望字段标签根据选择的单选按钮而改变,而不必复制整个部分。

代码段是:

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占位符。我需要想出一种方法,根据'YesRadiobButton'和'CTFYesRadiobButton'的选择,使 label1 变量。

有谁知道如何完成这项工作?

我尝试了以下内容但没有取得任何成功:

    var label1 = ViewData["TransferISA_Yes"].Equals(true) ? "name1" : "name2";

    var label1 = ViewData["TransferISA_Yes"] == "true" ? "name1" : "name2";

如果我可以帮助它,不要真的想使用Java / Jquery,但如果我必须这样做的话。

2 个答案:

答案 0 :(得分:0)

编辑 - 此答案仅适用于您的单选按钮位于同一页面

如果您想要数据的元素与标签位于同一页面上,那么您不应该真正使用ViewData ..这更像是将某些内容从控制器传递到视图

我认为jQuery是你最好的选择

$('input:radio[name=nameofradiobutton]').on('change', function()
{
   var selection = $(this).val();
   if(selection == something)
   {
     $("label[for='CurrentManager_TextBox']").text("some text");
   }
});

答案 1 :(得分:0)

感谢@krilovich的回复,但权力没有决定只有一组字段标签,所以这个问题不再有效。