javascript - 如何在重定向到下一页之前验证输入字段
问题描述
这是我的第一个 html 代码:
<form action = "indextable.html" method="GET" >
<p><input name = "ManagerId" class="form-control" id="ManagerId" type
="text" autocomplete="off" onkeyup="alphanumOnly(this)" maxlength="8"
placeholder="ManagerId"></input></p>
<p> <input type="submit" onclick ='return getRepoCount();'
value="submit">Submit</body></p>
</form>
<script>
function alphanumOnly(input){
var regex = /[^a-zA-Z0-9]/gi;
input.value = input.value.replace(regex, "");
}
function getRepoCount(){
var empid = document.getElementById("ManagerId").value;
alert(empid);
$.ajax({
url:'http://localhost:8088/JirasTrackingApp/reporter/
Reportees/ReporteeList/'+ empid,
type:'GET',
dataType: 'json',
success: function(result){
alert('hi');
return true;
},
error:function(result){
alert("Please enter an valid input");
return false;
}
});
}
</script>
在这里我无法进行 ajax 调用。当我运行程序时,它会显示 empid 警报。但是成功和错误的警报不会显示。就像它不是仅进行 ajax 调用一样。请告知我哪里错了。
解决方案
尝试使用它。还要确保 api 返回正确的 json。
<script>
function alphanumOnly(input){
var regex = /[^a-zA-Z0-9]/gi;
input.value = input.value.replace(regex, "");
}
function getRepoCount(){
var empid = document.getElementById("ManagerId").value;
alert(empid);
$.ajax({
url:'http://localhost:8088/JirasTrackingApp/reporter/
Reportees/ReporteeList/'+ empid,
type:'GET',
dataType: 'json',
success: function(result){
alert('hi');
return true;
},
error:function(result){
alert("Please enter an valid input");
return false;
},
complete: function () {
// Handle the complete event
alert("ajax completed " + "Your Data");
}
});
return false;
}
</script>
这是我从 API 得到的响应:
[{"UserId":"at12345","count":0,"Name":"Amras Taj"},
{"UserId":"AR12345","count":0,"Name":"Anaagh R"},
{"UserId":"MS12345","count":4,"Name":"Madhan S"}]
推荐阅读
- python - 是否可以在不删除 NaN 值的情况下修复“ValueError:无法将浮点 NaN 转换为整数”错误?
- kotlin - Kotlin - 带有溢出异常的类型转换
- c - STM32:将地址映射存储在数组中
- python - 如何将熊猫数据框中的边框添加到 html 表格行标题?
- jquery - 我可以在 jQuery 中创建一个“if”语句来检查一个集合中的一个类和另一个集合中的一个类吗?
- objective-c - 每秒调用一个方法不起作用
- build - 如何在 Azure DevOps 中将内部版本号添加到生成管道项目的名称中?
- for-loop - 没有条件的 Verilog For 循环给出错误:期望操作数
- azure - 存储帐户受 IP 限制时无法访问 CDN?
- jekyll - Travis CI 显示 Jekyll 生成的网站存在 0 个问题,但更改未显示在网站上