在同一页面处理多个表单 [英] Handling multiple forms in the same page

查看:134
本文介绍了在同一页面处理多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div class =dialogtitle = 基本对话框id =popup> 
< div class =text> < / DIV>
< / div>

< c:url value =/ $ {entity} / cadastravar =cadastra/>
...
< button type =submitclass =btn btn-lg btn-primary> cadastrar< / button>
< / form:form>

并且提交事件由此代码处理:

  $('form.form')。ajaxForm(function(data){
if(data =='')
$('#yes ').css('display','block');
else
$('#not')。css('display','block');

$('<< element>>')。each(function(){
this.reset();
});
});

如果我只想重置表单中的字段,任何人都知道<>应该是什么值我提交了,我可以在 div.dialog



中使用同一个类。我使用 form.form 这两个表单都被重置,如果我使用这个,我得到一个错误。任何想法来解决这个问题?

解决方案

每当你不确定访问插件内的元素时,你总是可以在一个插件中独立地初始化它们每个循环。



> <$ c $



  将是元素实例,您可以将其作为变量存储以传入插件。 > $('form.form')。each(function(){
/ *将this赋值给插件中可以使用的变量* /
var form = this;
$ (form).ajaxForm(function(data){
if(data ==''){
$('#yes').css('display','block');
} else {
$('#not')。css('display','block');
}
form.reset();
});
});


If I have something like this in some page in my project:

<div class="dialog" title="Basic dialog" id="popup">
    <div class="text"> </div>
</div>

<c:url value="/${entity}/cadastra" var="cadastra"/>
<form:form method="POST" action="${cadastra}" class="form" enctype="multipart/form-data">
...
    <button type="submit" class="btn btn-lg btn-primary">cadastrar</button>
</form:form>

and the submit event is handled by this code:

$('form.form').ajaxForm(function(data) {
    if(data == '')
        $('#yes').css('display', 'block');
    else
        $('#not').css('display', 'block');

    $('<<element>>').each(function(){
        this.reset();
    });
});

anyone knows what should be the value for <> if I want reset only the fields from the form I submit, and I could have a new form, with the same class, inside div.dialog?

If I use form.form both forms are reseted, and if I use this, I get an error. Any ideas to solve this?

解决方案

Whenever you are uncertain about access to elements within a plugin you can always initialize them individiually within an each loop.

Within each this will be the element instance and you can store it as variable to pass into plugin.

$('form.form').each(function () {
    /* assign "this" to variable that can be used inside plugin */
    var form = this;
    $(form).ajaxForm(function (data) {
        if (data == '') {
            $('#yes').css('display', 'block');
        } else {
            $('#not').css('display', 'block');
        }
        form.reset();
    });
});

这篇关于在同一页面处理多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆