单选按钮状态在鼠标上升或点击时没有更新
Radio button state not getting updated on mouseup or click
我使用一个自定义图像的单选按钮,所以我的单选按钮是隐藏的,我使用一个标签为该单选按钮,像这样:-
点击这里查看JS提琴链接
$('body').on('mouseup', 'label.q1odiv', function() {
if ($('input:radio[name="radio1"]').is(':checked')) {
// My code here
// I want to access currently checked radio button's value here
}
});
input[type=radio],
input[type=checkbox] {
display: none;
}
input[type=radio] + label {
display: block;
cursor: pointer;
background-image: url("http://www.clker.com/cliparts/8/V/0/V/G/Z/radiobutton-unchecked-th.png");
background-repeat: no-repeat;
background-size: contain;
margin-bottom: 2vw;
}
input[type=radio]:checked + label {
background-image: url("http://www.clker.com/cliparts/M/2/V/6/F/u/radiobutton-checked-sm-th.png");
}
.radio-btn {
width: 2vw;
height: 2vw;
}
.options-text-div {
margin: 0 3vw;
font-size: 2vw;
line-height: 2.2vw;
width: 25vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="options" id="options1">
<input type="radio" id="q1o0" name="radio1" checked="checked">
<label for="q1o0" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Kicks</div>
</label>
<input type="radio" id="q1o1" name="radio1">
<label for="q1o1" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Flip Flops</div>
</label>
<input type="radio" id="q1o2" name="radio1">
<label for="q1o2" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Stilletos</div>
</label>
<input type="radio" id="q1o3" name="radio1">
<label for="q1o3" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Boots</div>
</label>
<input type="radio" id="q1o4" name="radio1">
<label for="q1o4" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Wedges</div>
</label>
</div>
问题是每当我检查一个单选按钮时,我检查的单选按钮状态没有更新,而我在函数内。因此,我使用之前选中的单选按钮作为值,而不是当前选中的。
a)使用键盘的人无法访问您的解决方案
b)如果你监听输入的变化,它应该工作: https://jsfiddle.net/x31epgot/6/$('body').on('change', 'input', function(){
});
//By changing the specific radio you will get the id of that radio button
$('body').on('change', 'input:radio[name="radio1"]', function() {
console.log($('input:radio[name="radio1"]:checked').attr('id'));
});
input[type=radio],
input[type=checkbox] {
display: none;
}
input[type=radio] + label {
display: block;
cursor: pointer;
background-image: url("http://www.clker.com/cliparts/8/V/0/V/G/Z/radiobutton-unchecked-th.png");
background-repeat: no-repeat;
background-size: contain;
margin-bottom: 2vw;
}
input[type=radio]:checked + label {
background-image: url("http://www.clker.com/cliparts/M/2/V/6/F/u/radiobutton-checked-sm-th.png");
}
.radio-btn {
width: 2vw;
height: 2vw;
}
.options-text-div {
margin: 0 3vw;
font-size: 2vw;
line-height: 2.2vw;
width: 25vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="options" id="options1">
<input type="radio" id="q1o0" name="radio1" checked="checked">
<label for="q1o0" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Kicks</div>
</label>
<input type="radio" id="q1o1" name="radio1">
<label for="q1o1" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Flip Flops</div>
</label>
<input type="radio" id="q1o2" name="radio1">
<label for="q1o2" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Stilletos</div>
</label>
<input type="radio" id="q1o3" name="radio1">
<label for="q1o3" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Boots</div>
</label>
<input type="radio" id="q1o4" name="radio1">
<label for="q1o4" class="radio-btn q1odiv">
<div class="im-fell-dw-pica-font options-text-div ">Wedges</div>
</label>
</div>
相关文章:
- 更新Redux状态&然后在同一实例中获取更新状态
- 如果我不应该在组件WillUpdate中调用setState,如何更新状态(使用ReactJS)
- 如何在alt.js中使用源成功请求后更新状态
- 实现已验证用户更新状态的最佳方式,作为对系统中已更改状态的反应.(ASP.NET MVC网站)
- 如何使用Redux更新状态
- 使用 OnChange 事件更新状态具有延迟字符
- 在 redux 化简器中更新状态的正确方法
- 在反应本机中更新状态需要很长时间
- React - 如何在 AJAX 请求后更新状态
- onchange 事件更新状态,延迟为 1 个字符
- 在循环中调用 setState 仅更新状态 1 次
- react js和rails在具有活动记录关系的组件上更新状态
- React componentDidMount未更新状态
- 发送整个对象只是为了更新状态
- Javascript的问题.无法更新状态
- 当视图加载时更新状态
- Twitter REST API,更新状态
- 在React Router中,当页面改变时更新状态
- React-redux, connect函数不使用新数据更新状态
- React:将新插入的子元素返回给父元素以更新状态