我在使用 Javascript 和 PHP 将函数分配给 SweetAlert 时出错

问题描述

我想在另一个成功警报之后使用“甜蜜警报”显示输入警报,并且我想为其签名一个函数,该函数将检查写入的值是否与名为“codeNumber”的给定值匹配,该值基本上是一个 PHP变量。
回声' swal({ 图标:“成功”, title: "我们给你发了一封邮件!",text: "一封包含 6 位数代码的电子邮件已发送给您。",});

swal({
  title: "Write the 6-digits code here.",text: "Code:",type: "input",showCancelButton: true,cloSEOnConfirm: false,animation: "slide-from-top",}.then((inputValue) => {
  var rightcode = ' . $codeNumber . ';

  if (inputValue === null) {return false};
  if (inputValue === ""){
    swal.showInputError("You need to write something!");
    return false
  }

  if(inputValue === rightcode){
    swal({
      icon: "success",title: "Registred successfully!",text: "your account has been created!"
    });
  }

  if(inputValue !== rightcode){
    swal.showInputError("Wrong code!")
    return false;
  }
}));;
</script>
';

错误代码
{(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)}.then is not a function

解决方法

你把括号弄乱了。您正在对对象字面量调用 .then()

{
  title: "Write the 6-digits code here.",text: "Code:",type: "input",showCancelButton: true,closeOnConfirm: false,animation: "slide-from-top",}.then(/* ... /*)

您想要的是对 .then() 的结果调用 swal()

swal({
  title: "Write the 6-digits code here.",}).then((inputValue) => { // notice the closing parenthesis before .then()