简体   繁体   English

Jscript:函数不起作用

[英]Jscript : Function not working

I make a function to validate some required field on a form . 我提供了一个功能来验证表单上某些必填字段。 But the function is not functioning , i dont know what is wrong please help me out . 但是功能不起作用,我不知道怎么了,请帮帮我。

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>
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;
      }
    }

Kindly check this code you were missing to close form tag and also you were calling wrong function name. 请检查此代码,您是否缺少关闭表单标签,并且您在调用错误的函数名称。

<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>
 var x=document.forms["function_search_form"]["id"].value; var y=document.forms["function_search_form"]["name"].value 

This does get the .id and .name properties of your <form> , which both are the string "function_search_form" - which does not have a .value property. 这确实获得了<form>.id.name属性,它们都是字符串"function_search_form" -没有.value属性。 Instead, either get the inputs by ID : 相反,可以通过ID获取输入:

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

or by their name in the .elements collection : 或在.elements集合中按其名称命名:

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

See also Best Practice: Access form elements by HTML id or name attribute? 另请参阅最佳实践:通过HTML id或name属性访问表单元素?

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

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