javascript - 单击按钮启用和禁用单选按钮

标签 javascript jquery html

我有两个单选按钮

Popular

Others

两个按钮

Edit 

Cancel

最初,当我点击编辑按钮时,单选按钮将被禁用,单选按钮应该被启用,而当我再次点击取消按钮时,它应该被禁用。

目前我只能在单击编辑按钮时启用它,但是当我单击取消按钮时无法禁用它有人可以帮助我实现它谢谢!

http://jsfiddle.net/UhEut/452/

HTML

<div>
    <label>
        <input id='r_popular' class='pop_others' type='radio' name='section_pop_others' disabled checked />Popular
    </label>
    <label>
        <input id='r_others' class='pop_others' type='radio' name='section_pop_others' disabled />Others
    </label>
    <button class="edit">Edit</button>
    <button class="cancel">cancel</button>
</div>

JQuery

<script>
$(document).click('.edit',function()
{
    $('input[name=section_pop_others]').attr("disabled",true);
});

$(document).click('.cancel',function()
{
    $('input[name=section_pop_others]').attr("disabled",false);
});
</script>

最佳答案

为什么您的代码不起作用?

$(document).click('whatever here',function(){
// This block is run for every click event occuring anywhere on the document 
}

什么是正确的方法:

 $('Your selector').click(function(){
   // This block will run when click event occurs on Your selector
})

更改代码:

$(document).click('.cancel',function()
$(document).click('.edit',function()

为此:

    $('.cancel').click(function(){
    $('.edit').click(function()

** js Fiddle**

Go to this link to learn more about .click

关于javascript - 单击按钮启用和禁用单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33436223/

相关文章:

javascript - ANTLR:如何在算术运算中允许 boolean 值

javascript - 有太多的 setTimeout() 好吗?

javascript - 使用 jQuery 从这个 html 表单中获取文本区域的 ID

html - Primefaces p :calendar inside overlaypanel

javascript - 将点击监听器附加到类以切换所有子级的可见性

javascript - 如何在 JavaScript 中将字符串限制为一组特定的字符?

php - JavaScript 仅用于验证已检查的提交

javascript - 仅在第一个 li 内查找 h2

javascript - jQuery 过滤器显示数据属性任意或全部

html - 专门针对 HTML 内容优化的压缩算法?