Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 单选按钮mvc4的选定值_Jquery_Asp.net Mvc 4 - Fatal编程技术网

Jquery 单选按钮mvc4的选定值

Jquery 单选按钮mvc4的选定值,jquery,asp.net-mvc-4,Jquery,Asp.net Mvc 4,我正在从事MVC4项目。我对单选按钮有问题 下面是Radio按钮的html代码 @Html.RadioButton("Radiobtn", "true", new { id = "Radiobtn", @style = "width:auto;background:none;border:none" }) Yes @Html.RadioButton("Radiobtn", "false", new { id = "Radiobtn", @style = "width:auto;backgro

我正在从事MVC4项目。我对单选按钮有问题

下面是Radio按钮的html代码

@Html.RadioButton("Radiobtn", "true", new { id = "Radiobtn", @style = "width:auto;background:none;border:none" })
 Yes
 @Html.RadioButton("Radiobtn", "false", new { id = "Radiobtn", @style = "width:auto;background:none;border:none" })
 No
当我试图通过jquery获取所选值时,我只能看到第一个单选按钮的值

这是jquery代码

$('#Radiobtn').click(function () {
            if ($(this).val() == 'true') {
                alert('true');
            } else if ($(this).val() == 'false') {
                alert('false');
            }
        });

现在,仅当我选择第一个单选按钮时,警报才会显示。第二个单选按钮单击时没有发生任何事情??我不知道为什么它没有显示警惕

删除重复的id并尝试以下操作:

$('.Radiobtn').click(function () { //add class on each radiobutton here i take "Radiobtn"
            if ($(this).val()) {
                alert('true');
            } else {
                alert('false');
            }
        });
$('input[name=RadioBtn]').on('click', function() {
    // whatever...
});

两个单选按钮不能有相同的
id
。最好使用
(但我更喜欢使用组名)和
更改事件处理程序

$('.Radiobtn').change(function () {
            if ($(this).val() == 'true') {
                alert('true');
            } else if ($(this).val() == 'false') {
                alert('false');
            }
        });

您正在复制
id
属性,这是无效的,这就是为什么jQuery只在单击第一个单选键时才工作。尝试使用
,如下所示:

@Html.RadioButton(“Radiobtn”、“true”、new{id=“YesRadio”、@class=“radio”、@style=“width:auto;background:none;border:none”})
对
@RadioButton(“Radiobtn”、“false”、new{id=“NoRadio”、@class=“radio”、@style=“width:auto;background:none;border:none”})
不
$('.radio')。单击(函数(){
if($(this).val()=='true'){
警报(“真”);
} 
else if($(this).val()=='false'){
警报(“假”);
}
});

为单选按钮设置一个类,并且两个按钮都应该有不同的id

@Html.RadioButton("RadiobtnYes", "true", new { class= "Radio", @style = "width:auto;background:none;border:none" })
 Yes
 @Html.RadioButton("RadiobtnNo", "false", new { class= "Radio", @style = "width:auto;background:none;border:none" })
 No

$(".Radio").click(function(){
alert(this.val());
})
它将根据单击的单选按钮发出警报
true
false