简体   繁体   中英

How do you disable a button using an onclick javascript function?

I need to remove button click event after press. How can I edit the function so whenever it's clicked, it gets disabled then runs the url, or vise-versa.

<form id="edit" action="" method="post">
    <input type="submit" name="button" onclick="this.value='test...'; MyFunction();" id="1" class="button blue" value="Submit">
</form>
<script>
    function MyFunction() {
        var url = "editing.php";
        document.getElementById("edit").setAttribute('action', url);
        return false;
    }
    $(".blue").click(function(){
        $(".blue").removeClass("disabled");
        $(this).addClass("disabled");
    });
</script>

Remove the onclick , and do it the proper way :

<form id="edit" action="" method="post">
  <input type="submit" name="button" id="a1" class="button blue" value="Submit">
</form>
<script type="text/javascript">
$(function() {
    $('#a1').on('click', function() {
        $(this).val('test...').prop('disabled', true);
        $("#edit").attr('action', 'editing.php');
        return false;
    });
});
</script>
$(".blue").click(function(){
  $(this).attr('disabled', true);
});

Just set the disabled attribute

$('.blue').attr('disabled', 'disabled');

JSFiddle


As @doppelgreener pointed out, prop is equally valid here

$('.blue').prop('disabled', true);

JSFiddle

You can do this easily through jQuery using the attr() function:

$('.blue').click(function() {  
    $(this).attr('disabled', 'disabled');
});

By the way, I noticed that you're using jQuery. Why not 'jQuery-ify' your whole code:

function MyFunction() {
    var url = "editing.php";
    $("#edit").attr('action', url);
    return false;
}

$(".blue").click(function(){
    $(this).addClass("disabled").attr('disabled', true);
});

To make the .blue button clickable only once . The .one() method of jQuery is just the thing for you. Here is the documentation .

$("#edit > .blue").one("click", function() {
    //Code here will only run once.
}); 

"How can I edit the function so whenever it's clicked, it gets disabled then runs the url, or vise-versa."

I think you want the form to be submitted only once here; so:

<form id="edit" action="" method="post">
    <input type="submit" name="button" class="button blue" value="Submit" />
</form>

<script>
    //To make the form SUBMITTABLE ONLY ONCE:
    $("#edit").one("submit", function() { 
        $(".blue").prop("disabled", true); //you can still set the button disabled if you like
        $('#edit').attr('action', "editing.php"); //setting the action attribute here
    });
</script>

I don't know what your DOCTYPE is but if it's HTML4; you cannot have numeric ID for an HTML element. HTML5 seems to allow that. See this post .

Demo Fiddle here .

<input type="submit" name="button" onclick="$(this).attr('disabled', 'disabled')"/>
document.getElementById('1').setAttribute('disabled', 'disabled');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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