首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单选按钮启用/禁用不按预期工作

单选按钮启用/禁用不按预期工作
EN

Stack Overflow用户
提问于 2017-04-19 10:47:46
回答 5查看 2.7K关注 0票数 3

我试图使用以下功能切换我的radio按钮-- truefalse

但作为第一次点击,我没有得到任何回应。稍后它可以正常工作。现在要解决这个问题,这里有什么问题吗?

代码语言:javascript
复制
var switching = false;

$('button').on("click", function() {
  switching = switching == false ? switching = true : switching = false;
  console.log("switching", switching); //first click true but not works!
  $(":radio").attr("disabled", switching);
})
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="foo" value="Y" checked disabled>
<input type="radio" name="foo" value="N" disabled>
<input type="radio" name="foo" value="X" disabled>

<button>
  Switch Me
</button>

现场演示

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2017-04-19 10:51:18

您可以从true开始,然后像这样切换switching = !switching

代码语言:javascript
复制
var switching = true;

$('button').on("click", function() {
  switching = !switching
  $(":radio").attr("disabled", switching);
})
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="foo" value="Y" checked disabled>
<input type="radio" name="foo" value="N" disabled>
<input type="radio" name="foo" value="X" disabled>

<button>
  Switch Me
</button>

票数 8
EN

Stack Overflow用户

发布于 2017-04-19 10:58:30

或者你只需一行就可以做到:

代码语言:javascript
复制
$('button').on("click", function() {
    $(":radio").prop('disabled', function(){ return !this.disabled });
});
代码语言:javascript
复制
button.border { border:2px solid green; }
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" name="foo" value="Y" checked disabled>
<input type="radio" name="foo" value="N" disabled>
<input type="radio" name="foo" value="X" disabled>

<button>Switch Me</button>

票数 2
EN

Stack Overflow用户

发布于 2017-04-19 10:52:02

您的三元操作符不正确,应该是:

代码语言:javascript
复制
switching = switching == false ? true : false;

所以你的代码变成:

变量切换=假;

代码语言:javascript
复制
$('button').on("click", function(){
  switching = switching == false ? true : false;
  console.log( "switching", switching );
  $(":radio").attr("disabled", switching);

})

但是第一次单击将不起作用,因此您需要将其初始化为true。

代码语言:javascript
复制
var switching = true;

$('button').on("click", function(){
  switching = switching == false ? true : false;
  console.log( "switching", switching );
  $(":radio").attr("disabled", switching);

})
代码语言:javascript
复制
button.border{
border:2px solid green;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" name="foo" value="Y" checked disabled>
<input type="radio" name="foo" value="N" disabled>
<input type="radio" name="foo" value="X" disabled>

<button>
  Switch Me
</button>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43493535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档