jQuery iCheck 库:所选单选按钮的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18720926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iCheck library: value of selected radio button
提问by Clive van Hilten
jsFiddle here.
jsFiddle在这里。
I'm a novice at Javascript and having trouble following the documentation at the iCheckpage. I've followed the answer to what appears to be a very relevant StackOverflow question herebut cannot get an alert to pop up displaying the value I've selected. Can anyone point me in the right direction, please?
我是 Javascript 的新手,在遵循iCheck页面上的文档时遇到了问题。我已经按照这里似乎是一个非常相关的 StackOverflow 问题的答案进行了操作,但无法弹出显示我选择的值的警报。任何人都可以指出我正确的方向吗?
HTML
HTML
<p>Are you sure you want to have your details removed from our marketing list?</p>
<div id="unsubscribe_radio">
<ul>
<li>
<input type="radio" name="iCheck" value="yes">
<label>Yes</label>
</li>
<li>
<input type="radio" name="iCheck" value="no">
<label>No</label>
</li>
</ul>
</div>
Javascript
Javascript
$(document).ready(function () {
$('input').iCheck({
radioClass: 'iradio_flat-orange'
});
$("input:radio[name=iCheck]").click(function () {
var value = $(this).val();
alert("You clicked " + value);
});
});
回答by Anton
You must use iCheck like this
你必须像这样使用 iCheck
$(document).ready(function () {
$('input[name="iCheck"]').on('ifClicked', function (event) {
alert("You clicked " + this.value);
});
$('input').iCheck({
radioClass: 'iradio_flat-orange'
});
});
回答by simplylizz
Just another hacky way to get checked value with jQuery selectors:
使用 jQuery 选择器获取检查值的另一种方法:
$(".checked input[name=name-of-original-input]").val();
But AFAIK default class for checked values could be changed in configuration.
但是可以在配置中更改检查值的 AFAIK 默认类。
BTW, I think that iCheck isn't a good choice if it creates such issues with very simple functionality.
顺便说一句,如果 iCheck 使用非常简单的功能产生此类问题,我认为它不是一个好的选择。
回答by Sean L
I found the other answers to be useful. However, they are dependent on iCheck's 'ifClicked'. You may not want the value to pop up every time a new radio button is selected, but instead only when a user hits a specific button calling for the alert and corresponding value.
我发现其他答案很有用。但是,它们依赖于 iCheck 的“ifClicked”。您可能不希望每次选择新的单选按钮时都会弹出该值,而是仅在用户点击调用警报和相应值的特定按钮时才弹出该值。
For example:
例如:
$(document).on('click', '.buttonClass', function(event) {
return alert($('.radioClass:checked').val());
});
回答by Aditya Singh
Try this out:- http://jsfiddle.net/adiioo7/yhmu97qh/
试试这个:- http://jsfiddle.net/adiioo7/yhmu97qh/
JS:-
JS:-
$(document).ready(function () {
$('input').iCheck({
radioClass: 'iradio_flat-orange'
});
$('input').on('ifClicked', function (event) {
var value = $(this).val();
alert("You clicked " + value);
});
});
回答by Monzur
this works for me... try it
这对我有用......试试吧
HTML: <input type="checkbox" id=checkbox" name="checkbox" />
HTML: <input type="checkbox" id=checkbox" name="checkbox" />
jQuery: $( "#checkbox" ).prop("checked");
jQuery: $( "#checkbox" ).prop("checked");
回答by RenanStr
For radio buttons and current version of iCheck:
对于单选按钮和 iCheck 的当前版本:
<div class="row">
<div class="col-lg-1">
<div class="form-group">
<label>
<input type="radio" name="my-radio" class="flat-green my-radio" value="true"> Yes
</label>
</div>
</div>
<div class="col-lg-1">
<div class="form-group">
<label>
<input type="radio" name="my-radio" class="flat-red my-radio" value="false"> No
</label>
</div>
</div>
</div>
JS:
JS:
var isChecked = $("input[name=my-radio]:checked").val();
It will return undefined if no option is selected, true or false if selected.
如果没有选择任何选项,它将返回 undefined,如果选择,则返回 true 或 false。
回答by Allan
try this code:
试试这个代码:
$(".radio .checked input[type=radio]").val()