如何检查Razor中的RadioButton是否已选中? [英] How to check if a RadioButton is checked in Razor?

查看:100
本文介绍了如何检查Razor中的RadioButton是否已选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cshtml文件中有以下代码行

I have the following lines of code in a cshtml file,

<div class="YesNo">
    <label><input type="radio" name="YesNo_@pNum" value="Yes" />@yesLabel</label>
    <label><input type="radio" name="YesNo_@pNum" value="No" checked />@noLabel</label>
</div>

如何在Razor中使用if条件来检查是否选中了是"单选按钮,然后仅显示特定标签?

How can I use an if condition in Razor to check if "Yes" radiobutton is checked and then only display a certain label?

我的模型中有这两个属性,

I have these 2 properties in my model,

public string YesLabel { get { return COff.YesLabel ?? "Yes"; } }
public string NoLabel { get { return COff.NoLabel ?? "No"; } }

我在cshtml文件中也有2个变量,

I have 2 variables for that as well in the cshtml file,

var yesLabel = off.YesLabel;
var noLabel = off.NoLabel;

我的一个.cs文件中有这两个功能,

I have these 2 porperties in one of my .cs files,

public string YesLabel { get; set; }
public string NoLabel { get; set; }

我使用了此JS代码,并且可以正常工作,但是由于已经检查了我的否"单选按钮,所以我无法获得所需的输出,也就是说,如果我在访问页面后选择是",则看不到我的代码之所以有效,是因为始终在内存中选中否",

I used this JS code and it worked but since my "No" radioButton is already checked, I am not able to get the desired output,that is if I select "Yes" after visiting the page, i dont see my code working because "No" is always checked in the memory,

$(function () {                       
    var checkedRadio = $('[name="YesNo_@placementNumber"]:radio:checked').val();
    if (checkedRadio == "Yes") {
        $("#sel").html("Selected Value: " + checkedRadio);
    }
});

请提供任何帮助.

推荐答案

我不确定这是否是您的问题,但我认为您认为您有 name ="YesNo_ @ pNum" .我猜想 @pNum 是服务器端变量,输出是 name ="YesNo_X" .然后,在JS中,您有 $('[name ="YesNo_ @ placementNumber"] ,由于它不是 radio 的名称,因此将永远无法工作.

I'm not sure if this is your problem but I see that in your view you have name="YesNo_@pNum". I'm guessing that @pNum is a server side variable and the the output is name="YesNo_X". Then in the JS you have $('[name="YesNo_@placementNumber"], which will never work because that's not the name of the radio.

如果 placementNumber 是JS变量,则解决方案将使用以下方式更改选择器:

If placementNumber is a JS variable, the solution would be changing the selector with:

$('[name="YesNo_' + placementNumber + '"]:radio:checked')

如果您的JS中没有变量 placementNumber ,则可以向div中添加一个ID,包装收音机,然后选择它,如下所示:

If there is no variable placementNumber in your JS, you could add an id to the div wrapping the radios and then selecting it, like this:

<div class="YesNo" id="radios">
    <label><input type="radio" name="YesNo_@pNum" value="Yes" />@yesLabel</label>
    <label><input type="radio" name="YesNo_@pNum" value="No" checked />@noLabel</label>
</div>

然后获取检查值:

var checkedRadio = $('#radios :radio:checked').val();

我想您的另一件事可能是您希望在检查的单选更改时运行代码.我相信可能是因为:

The other thing I think your problem might be is that you want your code to run when the checked radio changes. I believe that might be it because of:

我看不到我的代码有效,因为在内存中总是选中否"

i dont see my code working because "No" is always checked in the memory

因此,您需要做的是:

$(function () {                       
    var radios = $('[name="YesNo_@placementNumber"]:radio');
    radios.on("change", function() {
        if ($(this).prop("checked")) {
            $("#sel").html("Selected Value: " + $(this).val());
        }
    });
});

这篇关于如何检查Razor中的RadioButton是否已选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆