单击按钮时启用和禁用收音机按钮
Enabling and Disabling Radio buttons on button clicks
我有两个单选按钮,即
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**
转到此链接了解更多信息。单击
$('.edit').click(function()
{
$('input[name=section_pop_others]').removeAttr("disabled");
});
$('.cancel').click(function()
{
$('input[name=section_pop_others]').attr("disabled","disabled");
});
修改了您的代码
您将点击事件附加到文档,而不是基于选择器的元素。
点击的两个选项:
- .click(处理程序函数)
- .click(事件数据,处理程序函数)
您正在将选择器传递给事件数据。
使用选择器将单击事件附加到目标对象。
$('.edit').click(function () {
$('input[name=section_pop_others]').attr("disabled", false);
});
$('.cancel').click(function () {
$('input[name=section_pop_others]').attr("disabled", true);
});
正如其他人所提到的,您使用click
甚至是错误的,但您还需要将事件处理程序附加到$(document).ready
才能使其工作。
这项工作:
HTML:
<div>
<input id="r_popular" class="pop_others" type="radio" name="section_pop_others" checked="checked" />Popular
<input id="r_popular" class="pop_others" type="radio" name="section_pop_others" />Others
</div>
<button class="edit">Edit</button>
<button class="cancel">cancel</button>
JavaScript:
$(document).ready(function() {
$('.pop_others').attr('disabled',true);
$('.cancel').click(function() {
$('.pop_others').attr('disabled',true);
});
$('.edit').click(function() {
$('.pop_others').attr('disabled',false);
});
});
$('.edit').on('click',function()
{
$('input[name=section_pop_others]').removeAttr('disabled');
});
$('.cancel').on('click',function()
{
$('input[name=section_pop_others]').prop("disabled",true);
});
Write click for button instead of writing to document it will trigger both the event same time.
试试这个
$(document).ready( function () {
$('.cancel').on ('click', function () {
$('input[name=section_pop_others]').attr("disabled",true);
});
$('.edit').on ('click', function () {
('input[name=section_pop_others]').attr("disabled",false);
});
});
JSFIDDLE
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 多个单选按钮组相互干扰
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- Javascript按钮下拉列表
- jquery试图按名称获取按钮位置
- 漂亮照片图片库中的Facebook赞按钮
- 扩展移相器按钮类不工作
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 单击按钮以等待单击按钮
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何从querySelectorAll中获取按钮类型
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 使用 jQuery 选中单选按钮不会触发收音机的 onclick 事件
- 正确向下拆卸收音机按钮
- 原型,听收音机按钮的变化
- 单击按钮时启用和禁用收音机按钮
- 我该怎么叫收音机按钮呢
- 点击[angularjs]按钮更改收音机型号