【问题标题】:Force user to input value on JavaScript prompt box强制用户在 JavaScript 提示框中输入值
【发布时间】:2016-04-07 15:00:41
【问题描述】:

我想知道如何强制用户在提示框中输入一个值,而不是将其留空。到目前为止,这是我的代码,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 ));
              }

【问题讨论】:

  • 你的代码有用吗?您的代码在此处无法正常工作的具体原因是什么?

标签: javascript loops while-loop


【解决方案1】:

作为警报,提示从安全角度来看是危险的,在不久的将来这些都将过时。 现在你不能强迫用户输入值。一种可能的解决方案是继续显示对话框,直到用户输入值。但是用户可以再次停止此对话框。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-10
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2018-03-24
    • 2017-04-03
    相关资源
    最近更新 更多