0

当验证失败时,我需要帮助来退出 JavaScript 代码。目前,当验证失败但 JavaScript 代码继续运行时,我收到了正确的错误消息。请在下面找到我的代码。谢谢

var CompPlanID=1;
var Component=2;
var TierNo=3;
var StartDate=4;
var EndDate=5;
var TierMin=6;
var TierMax=7;
var Rate=8;
var InvalidFlag = 0;
var BlankTextBox = '';

function DateCheck()
{
    var StartDateform= document.getElementById('tblTarget').rows[1].cells[StartDate].getElementsByTagName('input')[0].value;

    var EndDateform= document.getElementById('tblTarget').rows[1].cells[EndDate].getElementsByTagName('input')[0].value;

    var eDate = new Date(EndDateform);
    var sDate = new Date(StartDateform);

    if(StartDateform== BlankTextBox || EndDateform == BlankTextBox || sDate> eDate)
    {
        alert("Please ensure that the End Date is greater than or equal to the Start Date.");
    InvalidFlag = 1;
        }   
}

// 检查 pk 行是否为空

function CheckPkRow()
{
    var CompPlanIDform= document.getElementById('tblTarget').rows[1].cells[CompPlanID].getElementsByTagName('select')[0].value;

  if(CompPlanIDform== BlankTextBox)
  {
    alert("Please ensure that the primary key is not empty");
    InvalidFlag = 1;

  }

}

function Submit() 
{

    InvalidFlag = 0;
    CheckPkRow();
    DateCheck();    

//如果验证为真,则调用提交函数。

    if(InvalidFlag == 0 )
    {

    $('button_submit').click(); 
    alert('The new rate submitted');

    }

}
4

2 回答 2

2
function CheckPkRow()
{
    var CompPlanIDform= document.getElementById('tblTarget').rows[1].cells[CompPlanID].getElementsByTagName('select')[0].value;

  if(CompPlanIDform== BlankTextBox)
  {
    alert("Please ensure that the primary key is not empty");
    InvalidFlag = 1;
    return false;
  }
}
于 2013-07-15T09:44:35.377 回答
0

将您的代码更改为

function DateCheck()
{
    var StartDateform= document.getElementById('tblTarget').rows[1].cells[StartDate].getElementsByTagName('input')[0].value;
    var EndDateform= document.getElementById('tblTarget').rows[1].cells[EndDate].getElementsByTagName('input')[0].value;
    var eDate = new Date(EndDateform);
    var sDate = new Date(StartDateform);
    if(StartDateform== BlankTextBox || EndDateform == BlankTextBox || sDate> eDate)
    {
        alert("Please ensure that the End Date is greater than or equal to the Start Date.");
        return false;
    }  
    return true;        
}

function CheckPkRow()
{
    var CompPlanIDform= document.getElementById('tblTarget').rows[1].cells[CompPlanID].getElementsByTagName('select')[0].value;
    if(CompPlanIDform== BlankTextBox)
    {
        alert("Please ensure that the primary key is not empty");
        return false;
    }
    return true;
}

function Submit() 
{
    if(CheckPkRow() &&  DateCheck())
    {
        $('button_submit').click(); 
        alert('The new rate submitted');
    }
}
于 2013-07-15T09:47:31.297 回答