简体   繁体   English

使用jQuery / JS启用/禁用按钮

[英]enable/disable buttons with jquery/JS

I have a webpage where some data are shown. 我有一个网页,其中显示了一些数据。 If there is more data than can be shown at once I have a navigation bar enabling one to navigate to the next pages. 如果数据量超过一次显示的数量,我有一个导航栏,使您可以导航到下一页。 There are 4 buttons total; 共有4个按钮; "first page", "previous page", "next page" and "last page". “第一页”,“上一页”,“下一页”和“最后一页”。 If one is at page 1 the first 2 needs to be disabled, if one is at the last page the last 2 buttons needs to be disabled. 如果一个在第1页上,则前两个需要被禁用,如果一个在最后一页上,则后两个按钮需要被禁用。

Now I have seen bunch of example's (also here at stackOverflow) as to how to do it, problem is: none of them works! 现在,我已经看到了一堆示例(也在此处的stackOverflow上)如何进行操作,问题是:它们都不起作用!

I have tried (JS): 我尝试过(JS):

doc.getElmByID("button")
button.setAttribute('disabled', true/'disabled')
// (meaning both true and disabled have been tried with no luck).

jQuery: jQuery的:

$(button).attr('disabled', true/'disabled')
$(button).prop('disabled', true/'disabled')
$(button).button('disabled')

Now several of these does actually do something, they remove the "cursor=pointer" option, but the click event remains. 现在,其中一些确实可以执行某些操作,它们删除了“ cursor = pointer”选项,但单击事件仍然存在。

--EDIT-- - 编辑 -

HTML code HTML代码

            html += '<div id="naviLeft"><img type="button" id="First" class="grey" src="images/dobbelt-pil-disabled.png" title="First page"> </img>';
            html += '<img type="button" id="Previous" class="grey" src="images/pil-disabled.png" title="Previous page"></img></div>';
            html += '<div id="changeRecords"></div>';
            html += '<div id="naviRight"><img type="button" id="Next" class="grey" src="images/pil-r-disabled.png" title="Next page"> </img>';
            html += '<img type="button" id="Last" class="grey" src="images/dobbelt-pil-r-disabled.png" title="Last page"></img> </div>';
            document.getElementById("navi").innerHTML = html;

            $('#First').click(function(){currentTable.First(), setNavigation()});
            $('#Previous').click(function(){currentTable.Previous(), setNavigation()});
            $('#Next').click(function(){currentTable.Next(), setNavigation()});
            $('#Last').click(function(){currentTable.Last(), setNavigation()});

... ...

setNavigation = function()
{
if  (currentTable)
{
// currentTable.total and currentTable.currentPage are placeholders for current Page and total pages.

    var current = currentTable.currentPage;
    var total = currentTable.total;

    var first = $('#First');
    var previous = $('#Previous');
    var next = $('#Next');
    var last = $('#Last');

    if  (current == 1)
    {
        $(first).attr('src', 'images/dobbelt-pil-disabled.png');
        $(first).css('cursor', 'default');

        $(previous).attr('src', 'images/pil-disabled.png');
        $(previous).css('cursor', 'default');
    }

    if  (current > 1)
    {
        $(first).attr('src', 'images/dobbelt-pil.png');
        $(first).css('cursor', 'pointer');
        $(first).hover(function(){$(this).attr('src', 'images/dobbeltpil-chosen.png');},function(){$(this).attr('src', 'images/dobbelt-pil.png');});

        $(previous).attr('src', 'images/pil.png');
        $(previous).css('cursor', 'pointer');
        $(previous).hover(function(){$(this).attr('src', 'images/pil-chosen.png');},function(){$(this).attr('src', 'images/pil.png');});
    }

jQuery: jQuery的:

$(button).prop("disabled", true);
$(button).prop("disabled", false);

This seems wrong to me: 这对我来说似乎是错误的:

<img type="button" 

this should be: 这应该是:

<input type="image" 

and be sure to wrap it with doc ready handler: 并确保使用doc ready处理程序将其包装起来:

$(function(){
    $('[type="image"]').prop('disabled', true);
});

if your button is jquery ui is necessary to update the plugin 如果您的按钮是jquery ui,则需要更新插件

$("button").button();
...
...
..
..
$("button").attr("disabled","disabled").button("refresh");//disabled
$("button").removeAttr("disabled").button("refresh");//enabled

if not remove 如果不删除

button ("refresh")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM