【问题标题】:why run both of confirmation of if()else in javascript?whats solution? [closed]为什么要在 javascript 中同时运行 if()else 的确认?有什么解决方案? [关闭]
【发布时间】:2015-12-31 18:47:36
【问题描述】:

我编写代码来检查表单,如果用户输入所有信息,则运行确认显示“您确定要提交吗?”。我在 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 编程不显示两种形式。

但是我的问题是同时以两种形式运行,我无法以另一种形式阻止其中一个。

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

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

【问题讨论】:

  • 您的代码全部大写无效。你是用 iPhone 还是什么的?
  • 将您的代码包装在一个函数中(而不是让它全局运行)并将该函数绑定到每个表单的提交事件。这将确保每个表单都不会影响另一个表单。

标签: javascript block confirmation


【解决方案1】:
  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?");
}

【讨论】:

  • 感谢您的回复,但请再次运行确认。是的,您是对的。这些大写字母是因为在文本区域中输入以创建我的问题,所有 if ,返回和确认都是小写字母。您还有其他建议吗?
  • 我发现了问题。:-) 我两次调用这个函数。我发现在 1000 行编程之间搜索了 5 个小时,是的 codewizard,我使用平板电脑。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多