我想使用JavaScript使用JSON文件中的数据验证表单输入。目的是确保用户输入与JSON文件中的信息匹配的正确clnt_key
数据。我该怎么办?
<form>
<div class="form-group">
<input type="text" class="form-control" id="text1" aria-describedby="emailHelp" placeholder="Enter Client key">
</div>
<button type="button" onclick="javascript:validate()" class="btn btn-danger btn">Login</button>
</form>
这是似乎无法正常运行的JavaScript代码。
<script>
function validate() {
$.getJSON("clientkey.json", function(data) {
$.each(data.response.message.org_details, function(key, value) {
if (document.getElementById("text1").value == "CLNT_KEY") {
alert("validation succeeded");
location.href = "login.html";
} else {
alert("validation failed");
location.href = "fail.html";
}
});
});
}
</script>
这是我的JSON文件
{
"response": "yes",
"message": "Client Key Exists",
"org_details": {
"ENTY_ID": 31,
"ORG_LGL_NME": "Patterson Park Neighborhood Association",
"ORG_NME": "Patterson Park Neighborhood Association",
"ORG_DESC": null,
"CLNT_KEY": "PPNA2017",
"ADMN_KEY": "appna2017",
"FED_TAX_ID": null,
"ORG_INDST_CD": 20,
"ORG_ANNL_REVN_AMT": null,
"ORG_INCORP_CNTRY_CD": 840
}
}
答案 0 :(得分:0)
应该是:
$.each(data.response.message.org_details, function(key, value){
if (key === "CLNT_KEY" && document.getElementById("text1").value === value)
{
alert( "validation succeeded" );
location.href="login.html";
}
else
{
alert( "validation failed" );
location.href="fail.html";
}
});