简体   繁体   English

强制用户在JavaScript提示框中输入值

[英]Force user to input value on JavaScript prompt box

I was wondering how to force the user to input a value into the prompt box, not leaving it empty. 我想知道如何强制用户在提示框中输入一个值,而不是将其留空。 This is my code so far, the while loop is there to ensure that the 3 same answers are not asked. 到目前为止,这是我的代码,while循环可确保不询问3个相同的答案。

answer1 = parseInt(prompt(question1.q1+" "+" 1. " +question1.a1+" "+" 2. "+question1.a2+" "+" 3. "+question1.a3));
        while(answer1 < 1 || answer1 > 3){
              answer1 = parseInt(prompt("Please enter a number between 1 and 3: "+question1.q1+ " "+" 1. "+question1.a1+" "+" 2. "+question1.a2+" "+" 3. "+question1.a3 ));
              }

As alert, prompt are dangerous as security point of view, In near future these are going to be obsolete. 从安全角度来看,作为警报,提示是危险的,在不久的将来,这些将被淘汰。 For now you can't force user to input the value. 目前,您不能强制用户输入值。 One possible solution is to keep displaying dialog until user put the value. 一种可能的解决方案是一直显示对话框,直到用户输入该值为止。 But again user can stop this dialog. 但是用户可以再次停止此对话框。

function callPrompt(msg){
  tag = prompt(msg);
  if(tag == null){
     callPrompt("updated " + msg)
  }
 return tag;
 }
}
//call function to get value.
console.log(callPrompt("enter value"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM