jquery元素从按钮单击侦听器上的不同表单中选择

时间:2011-12-22 21:44:47

标签: jquery jquery-ui button jquery-selectors

好吧所以我在我网站的首页上有三个表单,我不是最好的jquery选择器或.each函数....所以我有j查询ui按钮链接到一个事件监听器发送一个ajax发布,但问题是我不能只指定我想要的形式!继承我的代码

<form>
fields......
fields......
fields......
fields......
<button>register</button>
</form>
<form>
fields......
fields......
fields......
fields......
fields......
<button>login</button>
</form>
<form>
fields......
fields......
fields......
fields......
fields......
<button>book</button>
</form>
$(function(){
    $('button').click(function(event) {
        $('form .... i wanna hide all forms once clicked .. working!!!').hide();
        event.preventDefault();
        $('pload').html('<img src="source/image/lbl.gif">');
             // heres where i get the data from all forms i just want one....
        var page = 'form.'+$('form').serialize();
        var huh = $('input:hidden').val();
        var data = 'pop='+huh+'&page='+page;
        $.post('source/php/bots/authorize.php',data,function(data){
                $('#pager_master_div').html(data);
                $('pload').html('');
            });
    });
});

2 个答案:

答案 0 :(得分:1)

选择点击按钮的表单:

$('button').on('click', function (event) {

    //stop the form from submitting normally since you are handling the submission with AJAX
    event.preventDefault();

    var $parentForm = $(this).parents('form'),//get this button's form
        page        = 'form.' + $parentForm.serialize(),//serialize the selected form
        huh         = $parentForm.find('input:hidden').val();//get the value of the hidden input in the selected form
});

以下是演示:http://jsfiddle.net/vrjxr/

答案 1 :(得分:0)

所以,替换:

var page = 'form.'+$('form').serialize();

有:

var page = "form."+$(this).closest('form').serialize();

所以你要得到哪个是点击按钮的最近父母(这个)。

$('input:hidden')选择器有问题吗?我无法确定问题,但如果是这样,您可以将$(this).closest('form')放入变量中,然后使用variable.find('input:hidden')