English 中文(简体)
摘录
原标题:Passing this from a form in jQuery
  • 时间:2012-04-20 14:35:54
  •  标签:
  • jquery
  • forms

我的职能是,在提交表格时检查某一类的所有要素,如果某一要素无效,则防止提交。 我利用“Qu”在网页上找到所有形式,并将这一功能附属于提交时的活动。

The problem with this, is as the function checks all elements of a certain type, it will prevent forms from being submitted if there are invalid elements in another form, even though it isn t being submitted so they don t matter.

To remedy this, I ve altered my function to check all elements of a certain type to accept a parameter of a form, and then only validate elements in that form. So far so good.

然而,如果该职能与“ j”放在该页上的所有表格上,我如何能够具体说明表格的参数?

当时,《酒类法》:

jQuery("form").submit(function() {
    return CheckElements();
});

我需要通过目前表格,无论是作为身份证还是物体。 类似:

jQuery("form").submit(function() {
    return CheckElements(SOME WAY OF PASSING THE FORM);
});

这是可能的,如果是的话,如何做到这一点?

最佳回答

您可以简单地将其当作一个论点:return CheckElements(this);

页: 1 职能可以这样看待:

function CheckElements(form) {
    $( :input , form).each(function() { /*do something*/ });
}

如果你希望有<条码>,则指您可使用<条码> return CheckElements.quest(s);援引您的职能;那么,你不需要该参数,也不需要您能够履行的职能,例如<条码>$:input , this)。

问题回答
$("form").submit(function() {
    return CheckElements(this);
});

在这种情况下,你必须首先分配一个班级或一个表格。 页: 1

<div class= error  style="display:none; color:red;">the entry by you was invalid</div>
<form action="XXXX" method="XXXX" class= myForm >
<!-- lets say you have following fields -->
name : <input type =  text  name =  name  class =  field1 >
age : <input type =  text  name =  age  class =  field2 >
<input type =  submit  id= submit_btn >
</form>

现在不是要求你履行使命,而是要求你履行使命。

$(function(){
$("#submit_btn").click(function(){
var msg = checkElement();// your function must return false on error
if(msg == FALSE)
{
$( .error ).show(); //create a div with style dispay:none which contains error message to show on error
}
else
{
var field1 = $( .field1 ).attr( value );//fetching the field1 content
var field2 = $( .field2 ).attr( value );//fetching the field2 content

//submit via ajax
$.ajax({
url:  relative path to the file eg. file.php ,
dta:  f1= +field1+ &f2= +field2,//placeyour own data here
type:  get or post ,
success:function()
{
alert( done );//perform your action
}
});
return false;
}
});
});

希望能帮助你





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

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.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签