单选按钮列表的javascript

时间:2009-12-01 13:26:24

标签: javascript jquery

我有一个Radiobutton(下面有6个项目)。我有一个搜索按钮。如果用户单击“搜索”按钮,则会获得所有结果。我使用.cs文件中的数据库绑定Radiobuttonlist的项目

条件1:现在如果用户选择了Radiobutton1 [item1]。现在如果用户再次点击Radiobutton1 [item1],那么它应该被取消选择。

如果在这里如何写一个函数onclick。我需要检查这种情况

要么你能在javascript或JQuery中为我提供解决方案,任何帮助都会很棒。期待一个解决方案  谢谢

2 个答案:

答案 0 :(得分:1)

这应该做你想要的。在页面上的每个单选按钮上提供启用/禁用功能。您可以更改选择器以仅匹配您感兴趣的那些无线电。

但我必须告诉你,启用/禁用radiobuttons是违反直觉的。你真的应该使用复选框。例如,我在使用你的网站时不会期望能够取消选中一个无线电按钮,因为这是不常见的行为。

$('input[type=radio]').each(function(i,e) {
    //save initial state
    $(e).data("oldstate", e.checked);
});

$('input[type=radio]').click( function (e) {
    var x = $(this);
    //if the current state is the same as the saved one toggle
    //else don't do anything
    if (this.checked == x.data("oldstate"))
        this.checked = !(this.checked);
    //save current state
    x.data("oldstate", this.checked);
});

答案 1 :(得分:0)

JQuery的

<script type="text/javascript"> 
    /* find _all_ input elements,
       where the attribute "type" = "radio",
       and add an onclick event to them */
    $('input[type=radio]').click( function {
        $(this).selected = !$(this).selected;
        return false;
    });
</script>
<input type="radio" id="radio1" name="radiogroup1" />
<input type="radio" id="radio2" name="radiogroup1" />

这应该只是让你知道如何处理这个问题。