Jquery and multiple forms on a page

前端 未结 4 2037
[愿得一人]
[愿得一人] 2021-01-17 06:30

I have a several forms that are output from a database on the same page. It works fine when I don\'t use ajax. When I use Jquery it will only work for the first form. Could

4条回答
  •  耶瑟儿~
    2021-01-17 07:18

    The problem is that selectors like $('.hardSoft') will select several elements (since there are multiple forms) and then .val() will take the value of the first. You could try finding the form using .parents('form') and then taking its children .children('.hardSoft').

    $('.updateSubmit').live('click', function() {
        var currentForm = $(this).parent();
        var hardSoft = currentForm.children('.hardSoft').val();
        // ... etc.
    

    On the other hand, this is a rather common task. Take a look at the jQuery Form plugin, which allows you to do the same using much less code. It's probably also more reliable and has several features that you might want to use later in your project.

提交回复
热议问题