使用ajax启用禁用jquery ui按钮

前端之家收集整理的这篇文章主要介绍了使用ajax启用禁用jquery ui按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我很难理解启用/禁用 jquery ui按钮的选项.

通常在过去我使用的任何按钮:

  1. jQuery(".mybutton").prop('disabled',false);

根据您是否要启用/禁用它,它将是false / true.

这似乎也适用于jquery ui按钮.我在做的是检查某个计数并在您第一次加载页面时禁用该按钮.

但有人可以通过删除某些东西来改变计数值(通过AJAX调用):

  1. //delete
  2. jQuery(document).on("click",".deletebutton",function() {
  3. if (confirm("Are you sure you want to delete?"))
  4. {
  5. var hash = jQuery(this).data("delete");
  6. var $this = jQuery(this);
  7. jQuery.ajax({
  8. url: "index.PHP?option=com_cam&task=delete&hash="+ hash +"&"+getToken()+"=1&format=raw",dataType: 'json',type: "POST",success: function(data){
  9. if (data.type == 'error')
  10. {
  11. //error message printed to user
  12. }
  13. else
  14. {
  15. $this.closest("tr").remove();
  16. //deleted so decrement and check if you have exceeded your limit
  17.  
  18. count=count-1;
  19. if (count >= limit)
  20. {
  21. //disable buttons
  22. jQuery( ".mybutton" ).button( "option","disabled",true );
  23. }
  24. else
  25. {
  26. //enable add buttons and hide message
  27. jQuery( ".mybutton" ).button( "option",false );
  28. //jQuery(".mybutton").attr('disabled',false);
  29. //jQuery(".mybutton").prop('disabled',false);
  30.  
  31. }
  32. }
  33. }
  34. });
  35. }
  36. });

然后这应该再次启用按钮. prop或attr或者这个post似乎都不适合我.只有当我这样做时:

  1. jQuery( ".mybutton" ).button( "option",false );

我想我不明白为什么道具在禁用我的按钮时可以在这种情况下不起作用?始终使用按钮setter是最佳做法吗?

在引擎盖下,该按钮已被禁用,但是,您看到的是一个按钮,其样式已由jQuery UI设置.

jQuery UI不使用disabled属性设置样式(如[disabled = disabled]).相反,它向按钮添加了两个类,其中包含禁用的按钮样式:ui-button-disabled,ui-state-disabled.

我知道有四种方法可行.

>推荐方式:调用jQuery(‘.mybutton’).button(‘disable’)
>你的方式(也推荐):调用jQuery(“.mybutton”).button(“option”,“disabled”,true);
>这很好:调用jQuery(“.mybutton”).prop(‘disabled’,true).button(‘refresh’).这会刷新按钮UI.
>不推荐的方法:直接从按钮添加/删除类. jQuery(“.mybutton”).addClass(‘ui-button-disabled ui-state-disabled’);

希望这能解释为什么它以这种方式工作.

猜你在找的Ajax相关文章