简体   繁体   English

需要 javascript 关于单选按钮的帮助

[英]Need help with javascript regarding radiobuttons

I want to disable the radiobutton the second time it is clicked..I want to put some code in the head..that when a radiobutton is clicked the second time,, it isnt marked anymore..我想在第二次单击单选按钮时禁用它..我想在头部放一些代码..当第二次单击单选按钮时,它不再被标记..

I want to check and uncheck the radiobutton with each click.我想在每次点击时检查和取消选中单选按钮。

Note: I generate 20 radiobuttons dynamically注意:我动态生成 20 个单选按钮

Take into account that it is a Radiobutton that is run on the server:考虑到它是在服务器上运行的 Radiobutton:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton.aspx

UPDATE: This is the only event that the RadioButton (asp WebControl run at="server") has:更新:这是 RadioButton (asp WebControl run at="server") 的唯一事件:

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    var rad = (CheckBox)sender;
    if (rad.Checked)
    {

        rad.Checked = false;
    }
}

I can uncheck it after each post back..but unless a post back doesnt happen, i cant select and deselect it.. Thats the problem:!我可以在每次回帖后取消选中它。但除非没有回帖,否则我不能 select 并取消选择它。这就是问题所在:! :( :(

I think you should keep with the standard use of RadioButtons, by saying this - use CheckBoxes instead, and clear all checkboxes if a different one is clicked...so when a checkbox is clicked the second time the standard uncheck will occur.我认为您应该通过这样说来保持 RadioButtons 的标准使用 - 改用CheckBoxes ,并在单击其他复选框时清除所有复选框......所以当第二次单击复选框时,将发生标准取消选中。

Id create an empty array. Id 创建一个空数组。 For every radiobutton you create, add its ID to the array as the key and set its value to 0. This will be the count for the specific button.对于您创建的每个单选按钮,将其 ID 作为键添加到数组中并将其值设置为 0。这将是特定按钮的计数。 Whenever a radiobutton is clicked, check the buttons ID against the array, if its less than 2, increment it.每当单击单选按钮时,对照数组检查按钮 ID,如果它小于 2,则增加它。 If not, disable the current button.如果没有,请禁用当前按钮。

EDIT: didn't realize you were checking if it was already checked, thoguht it was the number of times checked.编辑:没有意识到你正在检查它是否已经被检查过,虽然这是检查的次数。

$("#id").is(":checked") $("#id").is(":checked")

Should suffice应该够了

Another note...if all you're doing is disabling an element from being accessed by the user, you should handle this event on the client side.另一个注意事项...如果您所做的只是禁止用户访问某个元素,则应在客户端处理此事件。 You'll be using unnecessary server callback for functionality easily achievable via javascript.您将使用不必要的服务器回调来实现通过 javascript 轻松实现的功能。 Use jquery click event handlers which can be generic enough for you not to have to use identifiers, making the job that much easier.使用 jquery click 事件处理程序,它可以足够通用,您不必使用标识符,从而使工作变得更加容易。

Cheers干杯

if i get you right then all u need is a flag attribute of how many times u have clicked on the radio button and each time u click the radio the attribute increased by 1 and check the attribute every click if its 2nd time then disable the radiobutton如果我猜对了,那么您只需要一个标志属性,说明您单击单选按钮的次数,每次单击单选按钮时,该属性都会增加 1,如果是第二次单击,则检查属性,然后禁用单选按钮

so you need to generate ur radiobuttons like this所以你需要像这样生成你的单选按钮

<input type='radio' onclick='radioClick(this);' how_many_clicked='0' id='whatever id u need' name='whatever name u need' />

and create ur function in the head like the following并在头部创建你的 function ,如下所示

   function radioClick(e) {

    var flag = e.getAttribute('how_many_clicked');
    var times = Number(flag);
    times += 1;
    e.setAttribute('how_many_clicked', times.toString())
    if (times > 1) {
        e.checked = false;
        e.setAttribute('how_many_clicked', "0");
    }
    else {
        e.checked = true;
    }
}

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

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