本文介绍了如何根据进入&lt; span&gt;的值来限制表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在为userId可用性检查编写代码,现在我在提交表单时遇到问题,我的javascript代码是, function checkTeacherId(){ alert( 在checkTecherId()); var status = document .getElementById( status); if (status == Id in使用选择另一个) return false ; } html标签如下 < 输入 type = text class = 表单控件 名称 = teacherId id = teacherId 占位符 = 输入教师ID > < span id = status class = status > < / span > < 输入 type = 提交 class = btn btn-primary value = 提交 önclick = checkTeacherId(); / > span标签的值将来自servlet, if (contains){ out2.println( < font color = re d id = status>); out2.println( 正在使用的ID选择另一个); out2.println( < / font>); } else { out2.println( < font color = green id = status>); out2.println( Id Available); out2.println( < / font>); } 我想在这里做什么我不想提交表格如果我得到的值是Id in use choose another。我无法帮助我。解决方案 这里的span和font(来自servlet)具有相同的id。因此,您需要更改字体标记ID。然后你可以使用checkTeacherId()函数的以下修改版本: function checkTeacherId(){ var status = ( #status)文本(); if (status == Id in使用选择另一个) return false ; else return true ; } 这里我使用了jQuery。 (#status ).text()忽略span元素中的所有html标记,并仅返回文本。这意味着将从span元素中忽略字体标记,并且将返回文本Id in use choose another或Id Available。 I am writing code for userId availability check, now I am facing problem in submitting form, my javascript code is,function checkTeacherId(){alert(" in checkTecherId()");var status=document.getElementById("status");if(status=="Id in use choose another")return false;}html tag is as follow<input type="text" class="form-control" name="teacherId"id="teacherId" placeholder="Enter Teacher Id" ><span id="status" class="status"></span><input type="submit" class="btn btn-primary" value="Submit" önclick="checkTeacherId();" />The value of "span" tag will come from servlet as,if(contains){out2.println("<font color=red id=status>");out2.println("Id in use choose another");out2.println("</font>");}else{out2.println("<font color=green id=status>");out2.println("Id Available");out2.println("</font>");}what I want to do here I dont want to submit the form if I get the value as "Id in use choose another" . I am unable to do it please help me. 解决方案 Here span and font (coming from servlet) has the same id. So, you need to change font tag ids. Then you can use following modified version of your checkTeacherId() function:function checkTeacherId() { var status =("#status").text(); if (status == "Id in use choose another") return false; else return true; }Here I have used jQuery.("#status").text() ignores all the html tags inside the span element and returns only the text. That means font tag will be ignored from span element and the text "Id in use choose another" or "Id Available" will be returned. 这篇关于如何根据进入&lt; span&gt;的值来限制表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 12:37