Jscript:功能不起作用

时间:2014-02-04 10:21:46

标签: javascript

我创建了一个函数来验证表单上的一些必填字段。但功能不起作用,我不知道有什么问题请帮帮我。

SCRIPT:

 function validateForm()
    {
    var x=document.forms["function_search_form"]["id"].value;
    var y=document.forms["function_search_form"]["name"].value;
    if (x==null || x=="")
      {
      alert("ID must be filled out");
      return false;
      }
      if (y==null || y=="")
      {
      alert("Name must be filled out");
      return false;
      }
    }

FORM:

<p><a href="borang_pencarikerja.php">Tambah Pencari Kerja Baru</a>&nbsp;<a href="borang_pencarikerja.php"><img src="images/add_16.png" width="16" height="16" border="0" align="absbottom" /></a></p>
<form action="simpan_jobseeker.php" method="post" enctype="multipart/form-data" name="function_search_form" id="function_search_form" class="search_form" onsubmit="validate()">

<td colspan="2"><strong>ID</strong>
<input type="text" name="id" id="txt_no_kp" value="<?php echo $no_kp; ?>" />
<div id="autocomplete_no_kp" class="autocomplete"></div></td>

<td colspan="2"><strong>Name</strong>
<div id="div_nama"><input name="name" type="text" id="txt_nama" size="40" /></div></td>

3 个答案:

答案 0 :(得分:0)

function validateForm()
    {
    var x=document.getElementById("id").value;
    var y=document.getElementById("txt_nama").value;
    if (x==null || x=="")
      {
      alert("ID must be filled out");
      return false;
      }
      if (y==null || y=="")
      {
      alert("Name must be filled out");
      return false;
      }
    }

答案 1 :(得分:0)

请检查您缺少的代码以关闭表单标记,并且您还调用了错误的函数名称。

<script type="text/javascript">
function validateForm()
    {
    var x=document.forms["function_search_form"]["id"].value;
    var y=document.forms["function_search_form"]["name"].value;
    if (x==null || x=="")
      {
      alert("ID must be filled out");
      return false;
      }
      if (y==null || y=="")
      {
      alert("Name must be filled out");
      return false;
      }
    }
</script>

<p><a href="borang_pencarikerja.php">Tambah Pencari Kerja Baru</a>&nbsp;<a href="borang_pencarikerja.php"><img src="images/add_16.png" width="16" height="16" border="0" align="absbottom" /></a></p>
<form action="simpan_jobseeker.php"  method="post" enctype="multipart/form-data" name="function_search_form" id="function_search_form" class="search_form" onsubmit="return validateForm()">

<td colspan="2"><strong>ID</strong>
<input type="text" name="id" id="txt_no_kp" value="<?php //echo $no_kp; ?>" />
<div id="autocomplete_no_kp" class="autocomplete"></div></td>

<td colspan="2"><strong>Name</strong>
<div id="div_nama"><input name="name" type="text" id="txt_nama" size="40" /></div></td>

    <input type="submit" value="Submit"/>
    </form>

答案 2 :(得分:0)

var x=document.forms["function_search_form"]["id"].value;
var y=document.forms["function_search_form"]["name"].value

这会获得.id的{​​{1}}和.name属性,这两个属性都是字符串<form> - 它没有"function_search_form"属性。相反,要么获得输入by ID

.value

.elements collection中的姓名:

var x=document.getElementById("txt_no_kp").value;
var y=document.getElementById("txt_nama").value;

另见Best Practice: Access form elements by HTML id or name attribute?

相关问题