简体   繁体   English

JavaScript验证 - 中止ColdFusion操作页面加载

[英]JavaScript Validation - Aborting a ColdFusion action page load

I have a CF page, main.cfm , for searching records based a various input fields. 我有一个CF页面main.cfm ,用于根据各种输入字段搜索记录。 The search form 's action page points back to itself ( main.cfm ). 搜索form的操作页面指向自身( main.cfm )。

<form id="frm_srch" action="main.cfm" method="post" onSubmit="return validate_search_form()">

   <input id="order_date" name="OrderDate_srch" type="text"/>

I wired up the JS function validate_search_form() to onSubmit , and everything works ok except for the actual aborting of the page load. 我将JS函数validate_search_form()onSubmit ,除了实际中止页面加载外,一切正常。

function validate_search_form() {               
  var order_date = $("input[name='OrderDate_srch']").val();     

  var d = new Date(order_date);

  if( d == 'Invalid Date' && order_date != "" ) {
    alert("Invalid order date");
    returnToPreviousPage();
    //window.history.back();
    //event.returnValue = false;
    return(false);
  }

  alert("validations passed");
  //event.returnValue = true;
  return(true);     
}

I also tried creating an event handler to simply always prevent the page load, like so 我也尝试创建一个事件处理程序,以便始终阻止页面加载,就像这样

$('#frm_srch').submit(function (evt) {
  evt.preventDefault();
  window.history.back();
});

and everything else suggested in this post javascript to stop form submission 以及此帖javascript中建议的所有其他内容以停止表单提交

Is there anyway to make this happen from a CF page using a regular form ? 无论如何,使用常规formCF页面发生这种情况? Using cfform / cfinput doesn't work in my case b/c I cannot get the date field to toggle as disabled / enabled correctly (it's associated to a checkbox , and this behavior is for another post)? 使用cfform / cfinput在我的情况下不起作用b / c我无法正确地将date字段切换为disabled / enabled (它与checkbox相关联,此行为适用于其他帖子)? Thanks. 谢谢。

This is part of your existing function: 这是您现有功能的一部分:

if( d == 'Invalid Date' && order_date != "" ) {
    alert("Invalid order date");
    returnToPreviousPage();
    return(false);
}

alert("validations passed");
return(true);        

Here is a simpler version of js form validation: 这是一个更简单的js表单验证版本:

 if (something is wrong) {
     display something;
     return false;
 }
 else
     return true;

The first difference is the call to function returnToPreviousPage(); 第一个区别是调用函数returnToPreviousPage(); If the problem is on the current page, this function call may not be serving any useful purpose. 如果问题出现在当前页面上,则此函数调用可能无法用于任何有用的目的。 In fact, it might be messing you up. 事实上,它可能会搞砸你。

The next difference is that your function does not have the keyword else after the if block. 下一个区别是你的函数在if块之后没有关键字else While it won't matter if the return false command in your if block executes, it doesn't do any harm and prevents the value from being returned inadvertently. 虽然if块中的return false命令执行无关紧要,但它不会造成任何伤害,并且会阻止无意中返回该值。

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

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