Sweet Alert 2 模态框中的输入范围类型问题

3

有没有办法自定义输出到输入 (type range) 旁边的内容,以显示输入的值?我希望它能显示值 + %。

目前我尝试给输入框添加一个自定义类,并获取其 <output> 标签,然后像这样操纵其内部文本:

$(document).on('input', 'input', function () {
$(".customSwal output").text( $(".customSwal").val() + " %" ) 
});

没有特别成功的结果

$(document).ready(function(){

Swal.fire({
    
  icon: 'question',
  showCancelButton:'true',
  cancelButtonColor: '#d33',
  title: 'Test',
  text: 'Test',
  input: 'range',
  width: '25rem',
  inputAttributes: {
    min: 0,
    max: 100,
    step: 1,
   
    
  },
inputValue: 0,
  customClass: {
  input: 'customSwal',

}


})



});

//$(document).on('input', 'input', function () {
//$(".customSwal output").text( $(".customSwal").val() +  " %" ) 
//});
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@10"></script>
</head>
<body>
</body>
</html>

2个回答

1
你可以使用$(".customSwal output").text($(this).val() + " %")修改output元素的值。此外,在处理程序中,您还需要使用change事件。
演示代码:

$(document).ready(function() {

  Swal.fire({

    icon: 'question',
    showCancelButton: 'true',
    cancelButtonColor: '#d33',
    title: 'Test',
    text: 'Test',
    input: 'range',
    width: '25rem',
    inputAttributes: {
      min: 0,
      max: 100,
      step: 1,


    },
    inputValue: 0,
    customClass: {
      input: 'customSwal',

    }


  })
  //on load
  $(".customSwal output").text("0 %")

});
///when change occur 
$(document).on('input change', 'input[type=range]', function() {
  //get input value
  $(".customSwal output").text($(this).val() + " %")
});
<!DOCTYPE html>
<html>

<head>
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="//cdn.jsdelivr.net/npm/sweetalert2@10"></script>
</head>

<body>
</body>

</html>


1

您可以通过添加以下CSS代码,使范围的输出中包含%字符:

Swal.fire({
  input: 'range',
  inputAttributes: {
    min: 0,
    max: 100,
  },
  inputValue: 25
})
.swal2-range output::after {
  content: '%';
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script> 


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接