简体   繁体   English

ASP.NET单选按钮jQuery处理

[英]ASP.NET radio button jQuery handling

I am trying to figure out why my page doesn't fire the radio button change event. 我试图弄清楚为什么我的页面没有触发单选按钮更改事件。

Here's the script I have for it, it's supposed to show a div once the radio button is checked. 这是我的脚本,一旦选中单选按钮,它应该显示一个div。

        $("input[id$='radio1']").change(function () {
        if ($("input[id$=radio1]").is(':checked')) {
            $('#div1').removeClass('hidden');
        }
    });

what's wrong with this code? 此代码有什么问题?

$("input[id$='radio1']").change(function () {
    if ($(this).is(":checked")) {
        $("#div1").removeClass("hidden");
    }
});
$("input[id$='radio1']").change(function () {
    if ($(this).is(':checked')) {
        $('#div1').removeClass('hidden');
    }
});

You should use the this object in your function. 您应该在函数中使用this对象。

You need to use the this keyword inside the function, instead of asking jQuery to search for elements matching the selector again. 您需要在函数内部使用this关键字,而不是让jQuery重新搜索与选择器匹配的元素。

$("input[id$='radio1']").change(function () {
    if ($(this).is(':checked')) {
        $('#div1').removeClass('hidden');
    }
});

try asking in this way if the radio is checked. 尝试以这种方式询问是否已检查收音机。

if(document.getElementById('radio1').checked) {
    $('#div1').removeClass('hidden');
}

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

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