Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript SweetAlert2检测单选按钮选择_Javascript_Jquery_Sweetalert_Sweetalert2 - Fatal编程技术网

Javascript SweetAlert2检测单选按钮选择

Javascript SweetAlert2检测单选按钮选择,javascript,jquery,sweetalert,sweetalert2,Javascript,Jquery,Sweetalert,Sweetalert2,我正在使用SweetAlert2JavaScript库来显示弹出窗口。SweetAlert2是SweetAlert的一个分支,不再维护。SweetAlert2允许我添加这样的单选按钮 // inputOptions can be an object or Promise var inputOptions = new Promise(function(resolve) { resolve({ '#ff0000': 'Red', '#00ff00': 'Green',

我正在使用SweetAlert2JavaScript库来显示弹出窗口。SweetAlert2是SweetAlert的一个分支,不再维护。SweetAlert2允许我添加这样的单选按钮

// 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
  });
})
<input id="swal2-radio-1" type="radio" name="swal2-radio" value="3">
SweetAlert2为无线电输入指定“swal2无线电”名称,如下所示

// 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
  });
})
<input id="swal2-radio-1" type="radio" name="swal2-radio" value="3">
它应该打印到控制台,这样我就知道它工作了

以下是我目前掌握的代码:


是我做错了什么,还是因为SweetAlert2的工作方式,这不起作用?

您必须将事件逐个委托绑定到整个文档中

$(document).on("click",".swal2-container input[name='swal2-radio']", function() {
    var id = $('input[name=swal2-radio]:checked').val();
    console.log('id: ' + id);
});

检查

非常感谢你,我自己永远也不会明白这一点