2016-03-04 68 views
0

在我的請求AJAX中,在beforesend我禁用了所有按鈕,因爲在我看來是更好的安全選擇。 在成功我再次啓用所有按鈕。但我的頁面中有許多其他按鈕,我需要在成功請求後重新啓動默認禁用狀態。如何在ajax請求成功時恢復按鈕的初始值

有沒有一種方法可以獲取beforesend中設置的值,並在成功後設置該正確的禁用狀態?

因爲在我的代碼中,我啓用了所有按鈕都成功,但我需要恢復禁用狀態完全按照beforerequest。

低於我的簡單代碼。

  $.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> 
+0

不確定你的問題是什麼? 'js'的哪一部分沒有返回預期的結果? – guest271314

+0

我可以在發送之前得到DISABLED狀態,並在成功之後正確設置... 因爲我在beforesend上有disabled = true的按鈕。 我有一個按鈕菜單,他們在DISABLED中有diferents狀態。我可以在請求 –

+0

後得到恢復默認值是的,問題處的'js'似乎將'button'元素設置爲'disabled':'beforeSend'和'disabled'處爲'true','success'處爲'false'。在問題'js'沒有返回預期的結果?在'$ .ajax()'處沒有關閉')'? – guest271314

回答

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); 
} 
+0

這正是我需要的。非常感謝! \ o/ –

+0

爲了改善它,你可以嘗試添加一個數據而不是一個類,它不會是一個「調整」,它會工作相同。 – Suicideboy

+0

我會嘗試創建一個函數來捕獲將要執行的操作,因此啓用和禁用按ID標識它們的按鈕,您將會變得更好。 我並不知道如何開始,我現在有幾個。謝謝。 –