0

我想创建一个 javascript 来检查文本框的值,所以如果文本框为空白,它将不会进入下一页。并且检查后(如果所有条件都为真),它将返回文本框的结果。

我创建了这个 javascript:

function cekdata(myform)
{
    var id = document.myform.clientid.value;
        var nama = document.myform.nama.value;
        var divisi = document.myform.divisi.value;
        var way = document.getElementById('twoway').value;
        var ori = document.myform.lokasi.value;
        var desti = document.myform.tujuan.value;
        var ket = document.myform.keterangan.value;
        var tpergi = document.myform.tglb.value;
        var jpergi = document.myform.jamb.value;
        var mpergi = document.myform.menitb.value;
        var pegi = tpergi+', '+jpergi+':'+mpergi;
        var tplg = document.myform.tglp.value;
        var jplg = document.myform.jamp.value;
        var mplg = document.myform.menitp.value;
        var plg = tplg+', '+jplg+':'+mplg;
  if (document.myform.clientid.value == "")
  {
    alert("Please Fill Your ID");
    myform.clientid.focus();
    return false;
  }
  else
  if (document.myform.nama.value == "")
  {
    alert("Please Fill Passenger Name");
    myform.nama.focus();
    return false;
  }
  else
  if (document.myform.lokasi.value == "")
  {
    alert("Please Fill Origin Location");
    myform.lokasi.focus();
    return false;
  }
  else
  if (document.myform.tujuan.value == "")
  {
    alert("Please Fill Your Destination");
    myform.tujuan.focus();
    return false;
  }
  else
  if (document.myform.tglb.value == "")
  {
    alert("Please Fill Departure Date");
    myform.tglb.focus();
    return false;
  }
  else
  if (document.myform.novehicle.value == "")
  {
    alert("Please Fill Vehicle Number");
    myform.novehicle.focus();
    return false;
  }
  else
  if (document.myform.driverid.value == "")
  {
    alert("Please Fill Driver ID");
    myform.driverid.focus();
    return false;
  }
  else
  if(document.getElementById('twoway').checked)
  {
      if (document.myform.tglp.value == "")
      {
        alert("Please Fill Return Date");
        myform.tglp.focus();
        return false;
      }
      else
      if (document.myform.tglb.value > document.myform.tglp.value)
      {
        alert("Return date must bigger than departure date");
        myform.tglp.focus();
        return false;
      }
  }
  else
  {
        var a = window.confirm("CONFIRMATION :\nID : " +id+"\nName : "+nama+"\nDivision : "+divisi+"\nOne Way : "+way+"\nOrigin : "+ori+"\nDestination : "+desti+"\nNotes : "+ket+"\nDeparture : "+pegi+"\nArrived :"+plg);
        if (a==true)
        {
            return true;
        }
        else
        {
            return false;
        }
  }
}

我这样称呼这个函数:

<form name="myform" onsubmit="return cekdata(this);" method="POST" action="<?php $_SERVER["PHP_SELF"]; ?>">

但我得到的是确认框永远不会出现,它返回true(并转到下一页)。那么,如何改变这个条件,让我的确认框先出现,然后点击确定后,进入下一页,如果取消,什么都不做?

4

6 回答 6

3
*Just make slight changes in your javascript just passevent on form submit*


<input type="submit" value="Submit" name="submit" onClick="return cekdata(this,event);" />

当您的文本框为空或为空时,请写入 event.preventDefault() 而不是 return false;

function cekdata(myform,evt)
{
var id = document.myform.clientid.value;
    var nama = document.myform.nama.value;
    var divisi = document.myform.divisi.value;
    var way = document.getElementById('twoway').value;
    var ori = document.myform.lokasi.value;
    var desti = document.myform.tujuan.value;
    var ket = document.myform.keterangan.value;
    var tpergi = document.myform.tglb.value;
    var jpergi = document.myform.jamb.value;
    var mpergi = document.myform.menitb.value;
    var pegi = tpergi+', '+jpergi+':'+mpergi;
    var tplg = document.myform.tglp.value;
    var jplg = document.myform.jamp.value;
    var mplg = document.myform.menitp.value;
    var plg = tplg+', '+jplg+':'+mplg;
if (document.myform.clientid.value == "")
{
alert("Please Fill Your ID");
myform.clientid.focus();

evt.preventDefault(); }

于 2012-12-14T05:10:14.547 回答
0

我最好建议您将cekdata(this)放在您的提交标签而不是表单标签中。像

<input type="submit" value="Submit" name="submit" onClick="return cekdata();" />

而且当你这样做了

var id = document.myform.clientid.value;

那你为什么要再次使用相同的

if (document.myform.clientid.value == "")

更好的使用

var id = document.myform.clientid;

if (id.value == "")
{
   alert("Please Fill Your ID");
   id.focus();
   return false;
}
于 2012-12-14T05:04:34.197 回答
0

试试这个:

删除最后一个else 部分并粘贴到 javascript 函数末尾的行下方。

删除以下行:

var a = window.confirm("CONFIRMATION :\nID : " +id+"\nName : "+nama+"\nDivision : "+divisi+"\nOne Way : "+way+"\nOrigin : "+ori+"\nDestination : "+desti+"\nNotes : "+ket+"\nDeparture : "+pegi+"\nArrived :"+plg);

而是使用这个,

var a = window.confirm("Are you sure?");

if (a==true)
{
     return true;
}
else
{
     return false;
}
于 2012-12-14T05:04:43.087 回答
0

像这样更改代码:

function cekdata(myform)
{
    var id = document.myform.clientid.value;
    var nama = document.myform.nama.value;
    var divisi = document.myform.divisi.value;
    var way = document.getElementById('twoway').value;
    var ori = document.myform.lokasi.value;
    var desti = document.myform.tujuan.value;
    var ket = document.myform.keterangan.value;
    var tpergi = document.myform.tglb.value;
    var jpergi = document.myform.jamb.value;
    var mpergi = document.myform.menitb.value;
    var pegi = tpergi+', '+jpergi+':'+mpergi;
    var tplg = document.myform.tglp.value;
    var jplg = document.myform.jamp.value;
    var mplg = document.myform.menitp.value;
    var plg = tplg+', '+jplg+':'+mplg;
  if (id == "")
  {
    alert("Please Fill Your ID");
    myform.clientid.focus();
    return false;
  }
  else
  if (nama == "")
  {
    alert("Please Fill Passenger Name");
    myform.nama.focus();
    return false;
  }
  else
  if (ori == "")
  {
    alert("Please Fill Origin Location");
    myform.lokasi.focus();
    return false;
  }
  else
  if (desti == "")
  {
    alert("Please Fill Your Destination");
    myform.tujuan.focus();
    return false;
  }
  else
  if (tpergi == "")
  {
    alert("Please Fill Departure Date");
    myform.tglb.focus();
    return false;
  }
  else
  if(document.getElementById('twoway').checked)
  {
      if (tplg == "")
      {
        alert("Please Fill Return Date");
        myform.tglp.focus();
        return false;
      }
      else
      if (tpergi > tplg)
      {
        alert("Return date must bigger than departure date");
        myform.tglp.focus();
        return false;
      }
  }
  else
  {
        var a = window.confirm("CONFIRMATION :\nID : " +id+"\nName : "+nama+"\nDivision : "+divisi+"\nOne Way : "+way+"\nOrigin : "+ori+"\nDestination : "+desti+"\nNotes : "+ket+"\nDeparture : "+pegi+"\nArrived :"+plg);
        if (a==true)
        {
            return true;
        }
        else
        {
            return false;
        }
  }
}

然后它工作......

于 2012-12-14T05:31:14.383 回答
0

我会推荐使用 Jquery insead 的 Javascript。

这是我为您制作的简短而甜蜜的代码,请随意使用!

查询代码

$(document).ready(function() {
  $('#submit').click(function(){
     var id = $('#id').val();
     var dataString = 'id='+ id;
     if(id==''){
        alert("Please Fill Your ID");
        $('#id').focus();
        return false;  
         }
     else if(confirm("Are you sure? Your id = "+id))
        {
          $.ajax({
          type: "POST",
          url: "ajxRegistration.php",
          data: dataString,
          cache: false,
          success: function(e)
           {
          e.stopImmediatePropagation();
           }
     });
      return false;
    }
    });
})
于 2012-12-14T07:26:33.493 回答
0

试试这个 -

if(confirm('Are you sure?'))
    return true;
 else
    return false; 
于 2012-12-18T10:21:27.067 回答