【问题标题】:Javascript validation with Django使用 Django 进行 Javascript 验证
【发布时间】:2011-03-24 09:22:22
【问题描述】:

我的 Django 应用程序中有一个过滤选项。过滤是通过在下拉列表中选择的选项(姓名、位置、名称 EmployeeID、..etc)和在文本字段中输入的值来完成的。现在我想验证用户何时从下拉选项中选择 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 条件适用于所有进程。有人可以调查这个问题并给我一个解决方案吗?请发表评论以使我更清楚。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript python django dom-events


    【解决方案1】:

    检查了您的代码,如果我正确理解了您的问题,您希望这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2016-04-13
      • 2013-10-25
      • 2015-06-25
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      相关资源
      最近更新 更多