How to disable button using jquery in materialize css

一个人想着一个人 提交于 2019-12-12 15:09:11

问题


I am using jquery to disable a button in materialize css. According to documentation here, the way to disable button is simply by adding a class named 'disabled'.I think by simply add class 'disabled', the button will automatically disabled, but it is not. Here is my code:

html:

<button id='submit-btn' class="btn waves-effect waves-light submit red" type="button" name="action">

jquery:

$('#submit-btn').off().on('click', function(){
    $('#submit-btn').addClass('disabled');
});

回答1:


Try this

$('#submit-btn').removeClass("waves-effect waves-light submit").addClass('disabled');

Working Demo

$(document).ready(function() {
  $('#submit-btn').on('click', function() {
    $(this).removeClass("waves-effect waves-light submit").addClass('disabled');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/css/materialize.min.css" rel="stylesheet" />
<button id='submit-btn' class="btn waves-effect waves-light submit red" type="button" name="action">



回答2:


What about

$('#submit-btn').prop('disabled', true).addClass('disabled');



回答3:


to disable button you can use

$("#submit-btn").attr("disabled", "true");



回答4:


Create a variable which holds the button (say submitBtn), then add an event listener (say on DOMContentLoaded), then have the function disable the button with submitBtn.disabled = true;



来源:https://stackoverflow.com/questions/36174320/how-to-disable-button-using-jquery-in-materialize-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!