为什么在javascript中同时运行if()和else的确认?什么是解决方案

why run both of confirmation of if()else in javascript?whats solution?

本文关键字:else 确认 解决方案 什么 javascript if 运行 为什么      更新时间:2023-09-26

我写代码来检查一个表单,如果用户输入了所有信息,请运行确认显示"你确定要提交吗?"。我在onclick属性中使用这两个表单。我的代码是一样的:

Var strurl=window.location.search();
 If(strurl.search("click=edit")>0)
  {
 If(document.forms["frm"]["fname"].value="")
 { alert("please enter all information required");
  Return false;
}
Return Confirm("are you sure to submit?");
}
Else
{
If(document.forms["frm"]["tel_num"].value="")
  { alert("please enter all information required");
 Return false;
  }
Return Confirm("are you sure to submit?");

}

这段代码用于两个表单,其中一个表单在一个条件中显示,而不是通过php编程显示两个表单。

但我的问题是在两种形式下都运行确认,我无法在另一种形式下阻止其中一个。

我知道它可以分为两页,但出于某种原因,我应该在一页中使用这些代码。

我该怎么办才能解决这个问题?

  1. 将函数的所有标题大小写替换为小写,如(Confirm)、Return、Var和If Else
  2. .search应为.indexOf("click=edit")>-1
  3. 请适当地缩进您的代码,这将使您更好地理解

var strurl = window.location.search();
if(strurl.indexOf("click=edit") > -1) {
  if(document.forms["frm"]["fname"].value = "") {
    alert("please enter all information required");
    return false;
  }
  return confirm("are you sure to submit?");
}
else {
  if(document.forms["frm"]["tel_num"].value = "") {
    alert("please enter all information required");
    return false;
  }
  return confirm("are you sure to submit?");
}

我发现了问题。:-)这个函数我调用了两次。经过5个小时的搜索,我发现了1000行之间的代码。