【问题标题】:How restore initial values of buttons in ajax request success如何在ajax请求成功中恢复按钮的初始值
【发布时间】:2016-03-04 00:36:16
【问题描述】:

在我的请求 AJAX 中,在发送前我禁用了所有按钮,因为在我看来是更好的安全选择。 成功后,我再次启用所有按钮。但是我的页面中有许多其他按钮,成功请求后我需要重新启动默认禁用状态。

是否有一种方法可以在 beforesend 中设置获取值并在成功后设置此正确的禁用状态?

因为在我的代码中我成功启用了所有按钮,但我需要完全按照之前的请求恢复禁用状态。

在我的简单代码下方。

            $.ajax({
            url     :"/action/controller.php",                      
            type    :"POST",
            data    : data.serialize()
                    + '&'
                    + encodeURI('action')
                    + '='
                    + encodeURI(action)
                    + '&'
                    + encodeURI('tableid')
                    + '='
                    + encodeURI(tableid)
                    + '&'
                    + encodeURI('id')
                    + '='
                    + encodeURI(id),
            beforeSend: function(){
                //imgload.fadeIn('slow');
                $('button').attr('disabled', true);
                //console.log ($('button[name="action"]'));
                },
            success: function(retorno){
                //console.log( retorno );
                //msg(retorno, 'info');
                datagrid();
                $('form').unbind('submit').bind('submit');
                $('button').attr('disabled', false);
            }

html:

    <button id="formbutton" type="button"  data-action="back" data-tableid="<?php echo $tableid; ?>" data-id="<?php echo $id; ?>" class="btn btn-primary" title="Voltar" value="back">
    <span class="glyphicon glyphicon-chevron-left"></span>
</button>
<button id="formbutton" type="button" data-action="new" data-tableid="<?php echo $tableid; ?>" data-id="<?php echo $id; ?>" class="btn btn-primary" title="Novo" value="new">
    <span class="glyphicon glyphicon-plus-sign"></span>&nbsp;&nbsp;Novo
</button>
<button id="formbutton" type="submit" data-action="save" data-tableid="<?php echo $tableid; ?>" data-id="<?php echo $id; ?>" class="btn btn-primary" title="Salvar" value="save">
    <span class="glyphicon glyphicon-saved"></span>&nbsp;&nbsp;Salvar
</button>
<button id="formbutton" type="submit" data-action="delete" data-tableid="<?php echo $tableid; ?>" data-id="<?php echo $id; ?>" class="btn btn-primary" title="Delete" value="delete">
    <span class="glyphicon glyphicon-minus-sign"></span>&nbsp;&nbsp;Excluir
</button>
<button id="formbutton" type="submit" data-action="edit" data-tableid="<?php echo $tableid; ?>" data-id="<?php echo $id; ?>" class="btn btn-primary" title="Editar" value="edit">
    <span class="glyphicon glyphicon-edit"></span>&nbsp;&nbsp;Editar
</button>
<button id="formbutton" type="submit" data-action="duplicate" data-tableid="<?php echo $tableid; ?>" data-id="<?php echo $id; ?>" class="btn btn-primary" title="Duplicar" value="duplicate">
    <span class="glyphicon glyphicon-duplicate"></span>&nbsp;&nbsp;Duplicar
</button>
<button id="formbutton" type="submit" data-action="next" data-tableid="<?php echo $tableid; ?>" data-id="<?php echo $id; ?>" class="btn btn-primary" title="Proximo" value="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
</button>

【问题讨论】:

  • 不确定您的问题是什么? js 的哪一部分没有返回预期结果?
  • 我可以在发送前获得禁用状态,并在成功后正确设置...因为我在发送前有禁用 = true 的按钮。我有一个带按钮的菜单,它们在禁用时具有不同的状态。我可以在请求后恢复默认值
  • 是的,js at Question 似乎将button元素设置为disabledtruebeforeSenddisabledfalsesuccess。问题中的js 是否没有返回预期结果?缺少关闭 ) $.ajax()
  • 在我的脚本中,所有按钮设置成功后都启用,但我需要完全恢复,就像发送前一样。不懂?
  • 是的,jsbeforeSend 将所有button 元素设置为disabled,因为它使用选择器button。同样在success。您可以在 Question 中包含html 吗?您要将哪个特定的 button 元素设置为 disabledtrue ,然后将 false 设置为 success

标签: javascript jquery ajax


【解决方案1】:

您可以在刚刚禁用的按钮上添加一个类,并仅禁用启用的按钮,然后您只需使用您的类启用按钮:

beforeSend: function(){
  //imgload.fadeIn('slow');
  $('button:enabled').addClass('toEnable').attr('disabled', true);
  //console.log ($('button[name="action"]'));
},
success: function(retorno){
  //console.log( retorno );
  //msg(retorno, 'info');
  datagrid();
  $('form').unbind('submit').bind('submit');
  $('button.toEnable').removeClass('toEnable').attr('disabled', false);
}

【讨论】:

  • 要改进它,你可以尝试添加一个数据而不是一个类,它会少一点“调整”,它会起作用。
  • 我会尝试创建一个函数来捕捉将要执行的操作,然后启用和禁用通过 ID 识别它们的按钮,你真的会变得更好。我不知道如何开始,我现在有几个。谢谢。
猜你喜欢
  • 2012-01-12
  • 2019-07-09
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 2011-03-04
相关资源
最近更新 更多