English 中文(简体)
通过亚克斯功能要求验证
原标题:Calling validation through ajax function

I have two pieces of javascript that I would like to work together. I have a form that is submitted via ajax, and a form validation script, that I would like to work together to prevent the ajax form submission if the validation fails.

The ajax form submissionscript:

<script type="text/javascript">
$(document).ready(function(){$(".sendName").click(function(){
var jsuser=$("#jsuser").val();
var jemail=$("#jemail").val();
var jusid=$("#jusid").val();
$.post("recomend.php",{jsuser:jsuser,jusid:jusid,jemail:jemail},function(data){$("#loadName")
.html(data)
.effect( bounce , { times: 5, distance: 30 }, 300);
  });
 });
});
$( #start ).click(function() {
  $( #effect ).toggle( fast , function() {
   });
});
</script>

This code works perfectly, and the form is submitted.

我也有部分有效的验证法。 如果输入错误信息,错误信息就会在实地出现,但形式仍然可以提交。

通常形式的验证法也一样:

<form method="post" action="formprocess.php" name="form1" id="form1" onsubmit="return validate(this)">

因此,我需要知道如何做到:

onsubmit="return validate(this)"

in the ajax submission code, and not submit the form if there are errors.

最佳回答

为什么你们没有做以下事情?

<script type="text/javascript">

$(document).ready(function(){$(".sendName").click(function(){

var jsuser=$("#jsuser").val();
var jemail=$("#jemail").val();
var jusid=$("#jusid").val();
if(validate(jsuser, jemail, jusid)){
$.post("recomend.php",{jsuser:jsuser,jusid:jusid,jemail:jemail},function(data){$("#loadName")
.html(data)
.effect( bounce , { times: 5, distance: 30 }, 300);
  });
 });
});
$( #start ).click(function() {
  $( #effect ).toggle( fast , function() {
   });
});
}
</script>

在<条码>有效时,请作确认。

问题回答
$(".sendName").click(function(){
  if (!validate(this.form)) return;
  // rest of your function, including AJAX call
});

.form property is available on all form elements (HTMLSelectElement, HTMLOptionElement, HTMLInputElement, VDTextAreaElement, RUSButtonElement, RUSLabelElement, RUS FieldSetElement, RUSLegendElement, RUSObjectElement, and RUSIsIndexElement.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签