简体   繁体   English

jQuery同一页中的两种形式-值更改

[英]jQuery two forms in same page - value change

I've a list of all my users in a table. 我在表中列出了所有用户。 The last td element will contain a form, either a form for opening the account or a form for closing the account based on if the user is already been closed or not. 最后一个td元素将包含一个表单,用于打开帐户的表单或用于基于是否已关闭用户的帐户关闭表单的表单。 I'm using jQuery AJAX form plugin from http://malsup.com/jquery/form/ which is working. 我正在使用来自http://malsup.com/jquery/form/的 jQuery AJAX表单插件,该插件正在运行。 What I'd like to do is the change the value of button before the form is been submitted. 我想做的是在提交表单之前更改button的值。

Here's my JS for now: 这是我现在的JS:

$(document).ready(function() {

    $('form').ajaxForm({
        beforeSubmit: function() {
                $('form').find('input').val('Wait...');
            },
            success: function(data) { 
                $('body').html(data);
            }
        });
        return false;

    });

and here's my HTML markup: 这是我的HTML标记:

<td>
<?php if($user['closed'] == 0):?>
    <?php $attributes = ['class' => 'account']; ?>      
    <?php echo form_open('admin/closeAccount', $attributes);?>
        <input type="hidden" name="user_id" value="<?=$user['user_id']?>"/>
        <input type="hidden" name="user_email" value="<?=$user['email']?>"/>
        <input type="submit" name="close_account" class="btn btn-danger btn-sm" id="trigger" value="Close" >
    <?php echo form_close();?>

<?php else:?>
      <?php $attributes = ['class' => 'account'];?>
      <?php echo form_open('admin/openAccount');?>
        <input type="hidden" name="user_id" value="<?=$user['user_id']?>"/>
        <input type="submit" data-loading-text="Odota..." name="open_account" class="btn btn-success btn-sm" id="trigger" value="Open" >
      <?php echo form_close();?>
<?php endif ?>
</td>

The problem is that every time I try to submit the form, it will now change the value of all buttons to "Wait..." instead of just the one I clicked. 问题是,每次我尝试提交表单时,它现在会将所有按钮的值更改为“等待...”,而不仅仅是我单击的按钮。 I tried to replace $('form') with $(this) in $('form').find('input').val('Wait...'); 我试图用$('form') $('form').find('input').val('Wait...'); $(this)替换$('form') $('form').find('input').val('Wait...'); but that didn't help me at all. 但这根本没有帮助我。

Just try this code 只需尝试此代码

$('.btn').val('Wait...'); $('。btn')。val('等待...');

or 要么

$('.btn-danger').val('Wait...'); $('。btn-danger')。val('Wait ...');

or 要么

$('.btn-sm').val('Wait...'); $('。btn-sm')。val('Wait ...');

If i understood you right you need just change your selector to - $('input[type="submit"]'). 如果我理解的正确,则只需将选择器更改为-$('input [type =“ submit”]')。 So it will be - $('form').find('input[type="submit"]').val('Wait...'); 因此它将是- $('form').find('input[type="submit"]').val('Wait...');

Or use :submit selector 或使用:submit选择器

According to the plugin documentation ( http://malsup.com/jquery/form/#options-object ) $form passed to beforeSubmit callback should give you access to the current form being submitted. 根据插件文档( http://malsup.com/jquery/form/#options-object ),传递给beforeSubmit回调的$ form应该可以让您访问正在提交的当前表单。

try: 尝试:

$('form').ajaxForm({
    beforeSubmit: function(arr, $form, options) {
        $form.find('.btn').val('Wait...');
    },
    success: function(data) { 
        $('body').html(data);
    }
});

Try adding more specificity to your CSS selector. 尝试为CSS选择器添加更多特异性。 Inside your ajaxForm() call try this to change submit button value: 在您的ajaxForm()调用中,尝试更改提交按钮值:

$(this).find('input[type="submit"]').val('Wait...');

Your button has an id, why aren't you using that selector? 您的按钮有一个ID,为什么不使用该选择器?

$("#trigger").val("Wait...");

Is the form itself loaded via ajax? 表单本身是否通过Ajax加载? If so then delegate. 如果是这样,则委托。

$(document).on("customevent", "#trigger", function() {
    $(this).val("Wait...");
});

and then in the beforesubmit just $(document).trigger("customevent"); 然后在beforesubmit中只需$(document).trigger(“ customevent”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM