【问题标题】:Call 2 functions in is在 is 中调用 2 个函数
【发布时间】:2018-02-27 10:33:04
【问题描述】:

我有 2 个功能可以单独运行。在表单中使用它们。我可以在一个 onclick 事件中调用它们,但是我想将它们放在一个脚本中,第一个调用 validate() 但仅在信息正确时才调用第二个函数 display()。因此,如果调用了 validate() 并且信息不正确,它们会收到警报并返回表单 true,如果信息正确,则调用 display()。任何帮助表示赞赏。

 function validate() {

  // Get the value of the input field with id="QTY"
  var x = document.forms["confirm"]["QTY"].value;

// If x is Not a Number or less than one
  if (isNaN(x) || x < 1 ) {
      alert("Quantity - Minimum 1 required please");
   return true;
 }

 }



  function display()
 {
  var x=document.confirm.qty.value; 
  var y=document.confirm.price.value; 
  var z=document.confirm.total.value;

  var confirm = window.confirm('Quantity:' + x + '\nPrice Each: ' + y + '\nTotal Price: ' + z + '\n\nConfirm your order?' );
 }if(result)
 {
        // user has pressed ok

 }
else
        // user has pressed cancel
{
      document.getElementById("myform").reset();

    }

【问题讨论】:

  • if (validate()) { display(); } else { alert('whatever'); }?
  • 首先修复您的代码格式,其次如果验证通过,您可以在validate() 中调用display()
  • 谢谢两位。我对此很陌生,并且正在学习。你的建议我有点困惑。请问如何在 validate() 中调用 display ()。我试图理解。对不起,如果我听起来很愚蠢。

标签: javascript forms function


【解决方案1】:

如果验证通过,通常让 validate 返回 true。

function validate() {

    // Get the value of the input field with id="QTY"
    var x = document.forms["confirm"]["QTY"].value;

    // If x is Not a Number or less than one
    if (isNaN(x) || x < 1 ) {
      alert("Quantity - Minimum 1 required please");
      return false;
    }
    return true;
 }



 function display()
 {
    var x=document.confirm.qty.value; 
    var y=document.confirm.price.value; 
    var z=document.confirm.total.value;

    var confirm = window.confirm('Quantity:' + x + '\nPrice Each: ' + y + '\nTotal Price: ' + z + '\n\nConfirm your order?' );

    if(confirm)
    {
        // user has pressed ok

    }
    else
        // user has pressed cancel
    {
      document.getElementById("myform").reset();

    }
}

if (validate()) { display(); } 

如果您向我们提供有关 html 和胶水代码的更多信息,我们可以提供更好的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多