简体   繁体   English

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

[英]Variable Label Depending On Which Radio Button Is Selected

I have a webpage which displays a section with variable fields and labels. 我有一个网页,显示一个包含可变字段和标签的部分。 I am amending this page to add another radio button question. 我正在修改此页面以添加另一个单选按钮问题。

I want the field labels to change depending on which radio button is selected without having to duplicate the entire section. 我希望字段标签根据选择的单选按钮而改变,而不必复制整个部分。

Code snippet is: 代码段是:

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>

The template section is used for my CMS placeholders. 模板部分用于我的CMS占位符。 I need to come up with a way to make label1 a variable depending on the selection of the 'YesRadiobButton' and the 'CTFYesRadiobButton'. 我需要想出一种方法,使label1成为一个变量,具体取决于'YesRadiobButton'和'CTFYesRadiobButton'的选择。

Does anyone know how to get this done? 有谁知道怎么做到这一点?

I've tried the following without any success: 我试过以下但没有成功:

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

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

Don't really want to use Java/Jquery if I can help it but if I have to I will. 如果我可以帮助它,不要真的想使用Java / Jquery但是如果我必须的话。

EDIT - This answer only applies if your radio button is on the same page 编辑 - 此答案仅适用于您的单选按钮位于同一页面上

If the elements you want data from are on the same page as the label then you shouldn't really be using ViewData.. that is more for passing something from the controller to the view 如果您想要数据的元素与标签位于同一页面上,那么您不应该真正使用ViewData ..这更像是将某些内容从控制器传递到视图

jQuery is your best option here I think 我认为jQuery是你最好的选择

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM