[英]jquery ajax call after javascript validation function
I am trying to call my ajax code after my js validate() methods returns true on submit of the searchId button. 我的js validate()方法在提交searchId按钮后返回true后,尝试调用ajax代码。 But the ajax code is not executing.
但是ajax代码没有执行。 if I place an alert there it works fine.
如果我在那儿发出警报,则效果很好。 Where I am doing wrong?
我在哪里做错了? Please help!!
请帮忙!!
here is my javascript code 这是我的JavaScript代码
<script language="JavaScript">
function validate()
{
var msg = "";
//all my field validations are here
msg += "o Name is not a valid name.\n";
if (msg > "") {
alert(msg);
return false;
}
else {
return true;
}
}
$(document).ready(function(){
$("#simple-post").click(function()
{
$("#ajaxform").submit(function(e)
{
// getting the values of both firstname and lastname
var beginDate = $('input[name="txtBeginDate"]').val();
var endDate = $('input[name="txtEndDate"]').val();
var mdnVal = $('input[name="txtMsid"]').val();
// posting the values
var dataString = 'beginDate=' + beginDate + '&endDate=' + endDate+ '&mdnVal=' + mdnVal;
alert(dataString);
var formURL = $(this).attr("action");
//alert(formURL);
$.ajax(
{
url : formURL,
dataType:'json',
async: false,
data: dataString,
beforeSubmit: validate,
success:function(data){
queryObject = eval('(' + JSON.stringify(data) + ')');
queryObjectLen = queryObject.empdetails.length;
drawChart();
},
error : function(xhr, type) {
alert('server error occoured')
}
});
e.preventDefault(); //STOP default action
e.unbind(); //unbind. to stop multiple form submit.
});
$("#ajaxform").submit(); //Submit the FORM
});
});
Here is my HTML code 这是我的HTML代码
<form name="ajaxform" id="ajaxform" action="getData.jsp" onSubmit="return validate();" method="POST">
<Table>
<input type="submit" name="action" value="Search" id="searchId">
<input type="text" name="name" id="id" size="10" maxlength="10">
//other input fields
</table>
</form>
<button class="btn btn-info" id="simple-post">Run Code</button>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
UPDATE 更新
I have updated my code, i can submit my form, but the validation not working. 我已经更新了代码,可以提交表格,但是验证不起作用。 I am using beforeSubmit: validate, still i dont see my validation messages.
我正在使用beforeSubmit:验证,但仍然看不到验证消息。 Please help.
请帮忙。
I fxed the problem. 我解决了这个问题。 Added a Submit button markup and removed Run Code.
添加了一个提交按钮标记,并删除了运行代码。 Changed onsubmit to onclick on the submit button.
将onsubmit更改为onclick的提交按钮。 Again removed $("#simple-post").click(function() form my jquery code.This seems to be working fine now.
再次从我的jquery代码中删除$(“#simple-post”)。click(function()。这似乎现在工作正常。
Thanks to this post 感谢这篇文章
Validate a form with javascript before submitting with ajax? 在使用ajax提交之前,先使用javascript验证表单?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.