避免使用下一个url,直到类别名称是唯一的
avoiding to go for next url until the category name has been unique
这是Jsp代码。当我按下提交按钮时,我将使用javascript和ajax验证我的类别名称。它将在显示中询问类别名称。但与我使用ajax调用验证类别名称的唯一性相同,它将在蓝色时检查唯一性。当我第一次按下提交按钮,第二次按下时,它将转到主url
<form name="frm" action="createnewcatgoryBean.jsp" method="post">
<table style="padding: 5px 0px 0px 8px;">
<tr>
<th colspan="2">
<div style="width: width:271px; color:red;" id="validate"></div>
</th>
</tr>
<tr>
<th>Category Name<span>:</span></th><td><input id="cat" onblur="return validatenewcat()" type="text" name="category">
</td>
</tr>
<tr>
<th>Quotations form<span>:</span></th><td><input type="checkbox" name="quotations"></td>
</tr>
<tr>
<th>Agreement form<span>:</span></th><td><input type="checkbox" name="agreement"></td>
</tr>
<tr>
<th>Payment form<span>:</span></th><td><input type="checkbox" name="payment"></td>
</tr>
<tr>
<th>ETI<span>:</span></th><td><input type="checkbox" name="eti"></td>
</tr>
<tr>
<td colspan="2" style="float:right; padding-top:15px">
<input type="submit" value="Submit" onclick="return validatenewcat()" style="width: 60px;">
</td>
</tr>
</table>
</form>
这是我的ajax调用脚本和javascript
function validatenewcat()
{
var category = document.getElementById("cat").value;
if(category=="")
{
setTimeout(document.getElementById("validate").innerHTML="!PLz Enter The Category Name", 2000);
return false;
}
else
{
var url="catnamecheck.do?id="+category;
xmlhttp.open("post", url,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
var temp = xmlhttp.responseText;
if(temp!="")
{
document.getElementById("validate").innerHTML="!PLz Enter The Unique Category Name";
document.getElementById("cat").focus();
return false;
}
}
}
}
}
}
这是我的java代码
public Map<String, String> catuniqecheck(String id) {
Connection c = null;
PreparedStatement ps1 = null;
ResultSet rs1 = null;
String sql=null;
try{
c = JDBCHelper.getConnection();
if(c!=null)
{
Map<String, String> map = new HashMap<String, String>();
sql="select * from catgory where catgoryname=?";
ps1=c.prepareStatement(sql);
ps1.setString(1, id);
ps1.execute();
rs1=ps1.getResultSet();
if(rs1.next())
{
System.out.println("insdide of the catuniqecheck");
map.put("catgoryname",rs1.getString("catgoryname"));
JSONObject json = new JSONObject();
json.accumulateAll(map);
}
return map;
}
else
{
System.out.println("DB connection Established");
return null ;
}
}
catch (Exception e) {
// TODO: handle exception
return null ;
}
finally{
JDBCHelper.close(rs1);
JDBCHelper.close(ps1);
JDBCHelper.close(c);
}
}
这是我的servlet代码
System.out.println("inside success");
JSONObject json = new JSONObject();
json.accumulateAll(result);
response.setContentType("application/json");
response.getWriter().write(json.toString());
当我第二次按提交时,它将转到另一个url plz帮助我感谢u
我希望这就是面临的问题
<form name="frm" action="createnewcatgoryBean.jsp" method="post" onsubmit="return validatenewcat()">
<table style="padding: 5px 0px 0px 8px;">
<tr>
<th colspan="2">
<div style="width: width:271px; color:red;" id="validate"></div>
</th>
</tr>
<tr>
<th>Category Name<span>:</span></th><td><input id="cat" onblur="return validatenewcat()" type="text" name="category">
</td>
</tr>
<tr>
<th>Quotations form<span>:</span></th><td><input type="checkbox" name="quotations"></td>
</tr>
<tr>
<th>Agreement form<span>:</span></th><td><input type="checkbox" name="agreement"></td>
</tr>
<tr>
<th>Payment form<span>:</span></th><td><input type="checkbox" name="payment"></td>
</tr>
<tr>
<th>ETI<span>:</span></th><td><input type="checkbox" name="eti"></td>
</tr>
<tr>
<td colspan="2" style="float:right; padding-top:15px">
<input type="submit" value="Submit" style="width: 60px;">
</td>
</tr>
</table>
</form>
在表单标签中的提交时使用,而不是在提交类型中的点击时使用
var cat_array = new array();
var i=0;
function validatenewcat()
{
var is_dup_cat=false;
var category = document.getElementById("cat").value;
if(cat_array.length>=1){
for(var j=0;j<cat_array.length;j++){
if(cat_array[j]==category ){
is_dup_cat=true;
}
}
}
if(category=="" || is_dup_cat)
{
setTimeout(document.getElementById("validate").innerHTML="!PLz Enter The Category Name", 2000);
return false;
}
else
{
cat_array[i++]=category ;
var url="catnamecheck.do?id="+category;
xmlhttp.open("post", url,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
var temp = xmlhttp.responseText;
if(temp!="")
{
document.getElementById("validate").innerHTML="!PLz Enter The Unique Category Name";
document.getElementById("cat").focus();
return false;
}
}
}
}
}
}
相关文章:
- 使用onkeyup JS事件检查输入的值是否唯一
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- Lodash映射并返回唯一
- 在数据库中循环值时,为输入框获取唯一值
- 如何从数组中查找唯一对象
- 别名或以其他方式合并两个具有不同名称的相同对象原型
- 正在为循环创建唯一id
- 获取唯一值并使用javascript计算金额总和
- JavaScript:从对象数组中获取唯一值及其计数
- ng重复中的ng模型-初始化唯一作用域属性
- 生成唯一的ids js
- 创建具有2个唯一数字的Javascript数组
- 对中的函数调用进行排序是回调的唯一方法
- 如何在传单.js中显示每个choropleth弹出窗口的唯一名称和URL
- 在JavaScript中创建全局唯一ID
- 为dropdownlistfor(MVC)中的每个元素设置唯一id
- JavaScript-用唯一属性标识对象
- 动态创建唯一的复选框
- 如何为动态创建的元素分配唯一的id-Javascript
- Meteor针对唯一客户端集合的发布/订阅策略