[英]Javascript validation error with Django
我的Django应用程序中有一个过滤选项。 过滤是通过在下拉列表中选择的选项(名称,位置,名称,雇员ID等)以及在文本字段中输入的值来完成的。 现在我想验证用户何时从下拉选项中选择EmployeeID。如果在选择EmployeeID的同时未在文本框中输入任何值,则不应提交表单,并应显示警报以输入一些文本。 我创建了一个js函数来检查该字段是否为空,但是它无法按我想要的方式工作。 我将在这里粘贴我的代码。
<form name="myform" id="myformid" method="POST" >
Filter By:
<select name="choices" id ="choicesId" style="color: black; background-color: #BDBDBD" >
<option value="Name">Name</option>
<option value="Designation" >Designation</option>
<option value="EmployeeID" id="empid">EmployeeID</option>
<option value="Project" >Project</option>
<option value="DateOfJoin" >Date Of Join</option>
<option value="location" >Location</option>
<option value="email">Email</option>
<option value="skills">Skills</option>
</select>
<input id="textField "type="text" name="textField" style="color: black; background-color: #BDBDBD" >
<input type="button" value="Go" onclick="isEmpty();">
</form>
<table id="employeeTable" class="tablesorter">
<thead><tr><th>Employee List <!-- <input type="image" src="/static/sort_asc.gif " height="12" name="sortAscend"> --> </th></tr></thead> <br>
<tbody>
{%for emp in emp_list.object_list%}
<tr>
<td><a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{emp.id}} "> {{ emp.userName }} </a></td>
</tr>
{%endfor%}
</tbody>
</table></h4>
</div><br><br>
<a STYLE="text-decoration:none" href="http://10.1.0.90:8080/createEmployee/ ">Create New Employee </a>
<script type="text/javascript">
function isEmpty(){
if ((document.myform.choices.selectedIndex.value==''))
{
alert(document.myform.choices.options[document.myform.choices.selectedIndex].id);
alert("Hi");
document.myform.choices.focus();
/*document.getElementById('employeeIDfield').innerHTML = 'Please fill this field';*/
return true;
}
else
{
alert("Data entered");
document.getElementById('myformid').action = "http://10.1.0.90:8080/filter/";
document.getElementById('myformid').submit();
return false;
}
}
</script>
我能理解的是,即使输入javascript函数后,表单仍在提交。 else条件适用于所有过程。 有人可以调查这个问题并给我解决方案吗? 请给我更多的清晰度发表评论。 任何帮助,将不胜感激
检查了您的代码,如果我正确理解了您的问题,则需要这样做:
<form name="myform" id="myformid" method="POST" action="http://10.1.0.90:8080/filter/" onSubmit="javascript:return isEmpty();" >
Filter By:
<select name="choices" id ="choicesId" style="color: black; background-color: #BDBDBD" >
<option value="Name">Name</option>
<option value="Designation" >Designation</option>
<option value="EmployeeID" id="empid">EmployeeID</option>
<option value="Project" >Project</option>
<option value="DateOfJoin" >Date Of Join</option>
<option value="location" >Location</option>
<option value="email">Email</option>
<option value="skills">Skills</option>
</select>
<input id="textField" type="text" name="textField" style="color: black; background-color: #BDBDBD" value="" / >
<input type="submit" value="Go"/>
</form>
<table id="employeeTable" class="tablesorter">
<thead><tr><th>Employee List <!-- <input type="image" src="/static/sort_asc.gif " height="12" name="sortAscend"> --> </th></tr></thead> <br>
<tbody>
{%for emp in emp_list.object_list%}
<tr>
<td><a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{emp.id}} "> {{ emp.userName }} </a></td>
</tr>
{%endfor%}
</tbody>
</table></h4>
</div><br><br>
<a STYLE="text-decoration:none" href="http://10.1.0.90:8080/createEmployee/ ">Create New Employee </a>
<script type="text/javascript">
function isEmpty(){
var my_select = document.myform.choices;
var selected_index = my_select.selectedIndex;
var my_textfield = document.getElementById('textField');
if ((my_select[selected_index].value == 'EmployeeID') && ((my_textfield.value=='') || (my_textfield.value==null))) {
alert("Enter employee ID!");
my_textfield.focus();
return false;
}
else{
alert("Data entered");
return true;
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.