简体   繁体   English

javascript确认值不起作用

[英]javascript confirm value not working

i have a confirm and alert message box. 我有一个确认和警报消息框。 both are getting displayed at the right time. 两者都在正确的时间显示。

but in confirm box when i press cancel the applyaction_button_click is getting executed, which should not happen as i return false. 但是在确认框中,当我按取消时,applyaction_button_click正在执行,这不会发生,因为我返回false。

Similar is the case with alert box which returns false still applyaction_button_click is getting executed. 警报框返回错误的情况类似,仍然执行applyaction_button_click。

here is my code: 这是我的代码:

   protected void Page_Load(object sender, EventArgs e)
    {
                    ApplyAction_Button.Attributes.Add("onclick", "ShowConfirm();");
}

   protected void ApplyAction_Button_Click(object sender, EventArgs e)
    {
       // Action to be applied
    }

JS function: JS功能:

    function ShowConfirm() {
      //check if checkbox is checked if is checked then display confirm message else display alert message
      var frm = document.forms['aspnetForm'];
      var flag = false;
      for (var i = 0; i < document.forms[0].length; i++) {
          if (document.forms[0].elements[i].id.indexOf('Select_CheckBox') != -1) {
              if (document.forms[0].elements[i].checked) {
                  flag = true
              }
          }
      }

      if (flag == true) {
          if (confirm("Are you sure you want to proceed?") == true) {
              return true;
          }
          else {
              return false;
          }
      } else {
      alert('Please select at least Checkbox.')
      return false;
      }
  }

thanks 谢谢

When you have a client side event on an asp server control that has a server event and both get triggered with the same action, returning true or false on the client doesn't stop it from executing its server event. 如果在具有服务器事件的ASP服务器控件上有一个客户端事件,并且两者都被同一操作触发,则在客户端上返回true或false不会阻止它执行其服务器事件。

You could try to prevent the button's server event from executing by doing some kind of postback in your javascript code. 您可以尝试通过在JavaScript代码中执行某种回发操作来阻止执行按钮的服务器事件。

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

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