javascript - SweetAlert2 detect radio button select -
i using sweetalert2 javascript library show popups. sweetalert2 fork of sweetalert not maintained anymore. sweetalert2 allows me add radio buttons this
// inputoptions can 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 select something!'); } }); } }).then(function(result) { swal({ type: 'success', html: 'you selected: ' + result }); })
sweetalert2 give 'swal2-radio' name radio inputs, this
<input id="swal2-radio-1" type="radio" name="swal2-radio" value="3">
so try listen swal2-radio clicks this:
$("input[name='swal2-radio']").on("click", function() { var id = $('input[name=swal2-radio]:checked').val(); console.log('id: ' + id); });
it should print console know worked.
here code have far: https://jsfiddle.net/ayx0fkz3/
am doing wrong or not working because of how sweetalert2 works?
you have bind event delegation event binding 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
Comments
Post a Comment