最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable all the links and input buttons on a page using jquery - Stack Overflow

programmeradmin0浏览0评论

I am working in django and have created a class which has a field closed. When the user sets this value for a particular object, then I want to disable all buttons and links on the object_view page.

I am using the following jquery snippet, but it is not working. Can anybody help me find the mistake

<script type="text/javascript">
    $("a").css('cursor','arrow').click(function(){
        return false;
    });
    $("input").attr('disabled','disabled');
</script>

Update: Not working means that all the links and input buttons are still working, i.e. being directed to the correct page. I have included this code snippet in my <head> section.

I am working in django and have created a class which has a field closed. When the user sets this value for a particular object, then I want to disable all buttons and links on the object_view page.

I am using the following jquery snippet, but it is not working. Can anybody help me find the mistake

<script type="text/javascript">
    $("a").css('cursor','arrow').click(function(){
        return false;
    });
    $("input").attr('disabled','disabled');
</script>

Update: Not working means that all the links and input buttons are still working, i.e. being directed to the correct page. I have included this code snippet in my <head> section.

Share Improve this question asked Jan 13, 2012 at 20:18 SachinSachin 3,7829 gold badges58 silver badges97 bronze badges 3
  • Define "not working". Does it do anything? Are there errors on your console? – James Montagne Commented Jan 13, 2012 at 20:20
  • Works fine for me: jsfiddle/x9JCU – ComFreek Commented Jan 13, 2012 at 20:21
  • make sure you have jquery included – Grigor Commented Jan 13, 2012 at 20:24
Add a ment  | 

3 Answers 3

Reset to default 4

Try:

<script type="text/javascript">
$(document).ready(function(){
  $("a").css("cursor","arrow").click(false);
  // for jquery older than 1.4.3, use the below line
  // $("a").css("cursor","arrow").click(function(){
  //   return false;
  // });

  $(":input").prop("disabled",true);
  // for jquery older than 1.6, use the below line
  // $(":input").attr("disabled","disabled");
});
</script>

You need the $(document).ready(...) so that the code doesn't run until the elements exist on the page.

I guess you could turn off all links (that is anchor tags) with something like this:

$("a").click(function (e) {
   e.preventDefault();
});

This should work for buttons and other inputs

$(":input").attr('disabled','disabled');

You need to wrap the whole thing in:

$(function() {

});

OR

$(document).ready(function () {

});

To limit the input to buttons:

$('input[type="button"]').attr("disabled","disabled");

Otherwise you disable all input elements.

发布评论

评论列表(0)

  1. 暂无评论