如何使用jquery禁用页面上的所有链接和输入按钮

发布于 2024-12-27 02:00:24 字数 462 浏览 1 评论 0原文

我正在 django 中工作,并创建了一个具有字段闭合的类。当用户为特定对象设置此值时,我想禁用 object_view 页面上的所有按钮和链接。

我正在使用以下 jquery 片段,但它不起作用。任何人都可以帮我找到错误

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

更新:不工作意味着所有链接和输入按钮仍然有效,即被定向到正确的页面。我已将此代码片段包含在我的 部分中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2025-01-03 02:00:24

尝试:

<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>

您需要 $(document).ready(...) 以便在元素存在于页面上之前代码不会运行。

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.

☆獨立☆ 2025-01-03 02:00:24

我想你可以用这样的东西关闭所有链接(即锚标记):

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

这应该适用于按钮和其他输入

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

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');
南风几经秋 2025-01-03 02:00:24

您需要将整个内容包装在:

$(function() {

});

$(document).ready(function () {

});

将输入限制为按钮:

$('input[type="button"]').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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文