简体   繁体   English

如何检查Razor中的RadioButton是否已选中?

[英]How to check if a RadioButton is checked in Razor?

I have the following lines of code in a cshtml file, 我在cshtml文件中包含以下代码行,

<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>

How can I use an if condition in Razor to check if "Yes" radiobutton is checked and then only display a certain label? 如何在Razor中使用if条件来检查是否选中了“是”单选按钮,然后仅显示特定标签?

I have these 2 properties in my model, 我的模型中有这两个属性,

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

I have 2 variables for that as well in the cshtml file, 我在cshtml文件中也有2个变量,

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

I have these 2 porperties in one of my .cs files, 我的一个.cs文件中有这两个杂物,

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

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, 我使用了此JS代码,并且可以正常工作,但是由于已经检查了我的“否”单选按钮,所以我无法获得所需的输出,也就是说,如果我在访问页面后选择“是”,则我的代码无法正常工作,因为“始终会在内存中检查“否”,

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

Please any help is appreciated. 请任何帮助表示赞赏。

I'm not sure if this is your problem but I see that in your view you have name="YesNo_@pNum" . 我不确定这是否是您的问题,但在您看来,您的name="YesNo_@pNum" I'm guessing that @pNum is a server side variable and the the output is name="YesNo_X" . 我猜测@pNum是服务器端变量,输出为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 . 然后在JS中,您有$('[name="YesNo_@placementNumber"] ,由于它不是radio的名称,因此将永远无法工作。

If placementNumber is a JS variable, the solution would be changing the selector with: 如果placementNumber是一个JS变量,则解决方案将通过以下方式更改选择器:

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

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: 如果没有可变placementNumber在你的JS,你可以一个ID添加到div包裹的电台,然后选择它,就像这样:

<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>

And then for getting the checked value: 然后获取检查值:

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 我看不到我的代码正常工作,因为内存中始终选中“否”

So, what you need to do is: 因此,您需要做的是:

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

Razor is a server side tool. Razor是服务器端工具。 It cannot work after the page is already loaded. 页面已加载后无法使用。 It is not like on webforms when there is 'back and forth' between the client and the server. 当客户端和服务器之间存在“来回”时,这与Webforms上的情况不同。

With JS you can do the check, also with an event to know when the input was changed. 使用JS,您可以进行检查,还可以通过事件知道何时更改了输入。

You store your labels on a JS var, or place them on a hidden span, toggle them on and off according to your checkbox. 您可以将标签存储在JS var上,或将其放置在隐藏的范围内,然后根据您的复选框将其打开和关闭。

For example: 例如:

var noLabel = '@noLabel';
var yesLabel = '@yesLabel';

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

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