admin管理员组文章数量:1401484
I am using SweetAlert2 javascript library to show popups. SweetAlert2 is a fork of SweetAlert which was not maintained anymore. SweetAlert2 allows me to add radio buttons like this
// inputOptions can be an object or Promise
var inputOptions = new Promise(function(resolve) {
resolve({
'#ff0000': 'Red',
'#00ff00': 'Green',
'#0000ff': 'Blue'
});
});
swal({
title: 'Select color',
input: 'radio',
inputOptions: inputOptions,
inputValidator: function(result) {
return new Promise(function(resolve, reject) {
if (result) {
resolve();
} else {
reject('You need to select something!');
}
});
}
}).then(function(result) {
swal({
type: 'success',
html: 'You selected: ' + result
});
})
SweetAlert2 give 'swal2-radio' name to the radio inputs, like this
<input id="swal2-radio-1" type="radio" name="swal2-radio" value="3">
So I try to listen for swal2-radio for any clicks like this:
$("input[name='swal2-radio']").on("click", function() {
var id = $('input[name=swal2-radio]:checked').val();
console.log('id: ' + id);
});
It should print to console so that I know it worked.
Here is the code that I have so far: /
Am I doing something wrong or is this not working because of how SweetAlert2 works?
I am using SweetAlert2 javascript library to show popups. SweetAlert2 is a fork of SweetAlert which was not maintained anymore. SweetAlert2 allows me to add radio buttons like this
// inputOptions can be an object or Promise
var inputOptions = new Promise(function(resolve) {
resolve({
'#ff0000': 'Red',
'#00ff00': 'Green',
'#0000ff': 'Blue'
});
});
swal({
title: 'Select color',
input: 'radio',
inputOptions: inputOptions,
inputValidator: function(result) {
return new Promise(function(resolve, reject) {
if (result) {
resolve();
} else {
reject('You need to select something!');
}
});
}
}).then(function(result) {
swal({
type: 'success',
html: 'You selected: ' + result
});
})
SweetAlert2 give 'swal2-radio' name to the radio inputs, like this
<input id="swal2-radio-1" type="radio" name="swal2-radio" value="3">
So I try to listen for swal2-radio for any clicks like this:
$("input[name='swal2-radio']").on("click", function() {
var id = $('input[name=swal2-radio]:checked').val();
console.log('id: ' + id);
});
It should print to console so that I know it worked.
Here is the code that I have so far: https://jsfiddle/ayx0fkz3/
Am I doing something wrong or is this not working because of how SweetAlert2 works?
Share Improve this question edited Apr 21, 2017 at 9:47 Limon Monte 54.5k49 gold badges189 silver badges220 bronze badges asked Sep 22, 2016 at 14:01 niko craftniko craft 2,9975 gold badges47 silver badges75 bronze badges1 Answer
Reset to default 5you have to bind event by delegation event binding with whole document.
$(document).on("click",".swal2-container input[name='swal2-radio']", function() {
var id = $('input[name=swal2-radio]:checked').val();
console.log('id: ' + id);
});
Check Fiddle Link
本文标签: javascriptSweetAlert2 detect radio button selectStack Overflow
版权声明:本文标题:javascript - SweetAlert2 detect radio button select - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744285068a2598839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论