javascript - 删除JQuery中的单个单词属性

标签 javascript jquery html bootstrap-4

我的 html 网页上有一个 Bootstrap 4 按钮。 w3schools ( https://www.w3schools.com/bootstrap4/bootstrap_buttons.asp ) 表示,通过以下方式包含一个按钮将使其显示为禁用状态,即不可点击。

<button type="button" class="btn btn-primary" disabled>Disabled Primary</button>

我想设置按钮的表单,以便当单击它时,它会被禁用。以下是我迄今为止所掌握的相关部分:

 $(document).on('submit', '.Validate', function(e){
        e.preventDefault();
        var form = $( this );
        form.attr('disabled')
        $.ajax({
            type:'POST',
            url:'/workflowautomator/setready/',
            data:{
                name: (form.find('input[class="name"]')).val()
            },
            success:function(){
                form.removeAttr('disabled')
            }
        });
 });

但是,我看到的 .attr 的用法更像

$( "#greatphoto" ).attr( "title", "Photo by Kelly Clark" );

其中有属性的名称和值,而不是在上面,其中“disabled”一词单独出现在按钮开始标记中。难道“残疾人”这个词实际上不是一个合适的属性吗? w3schools 是这样描述的。我怎样才能让这段代码工作?

最佳答案

这不是您需要使用的正确的:

form.attr('disabled')

替换为:

form.find(".btn-primary").addClass("btn-disabled").prop("disabled", true);

您必须使用.prop()添加和删​​除属性属性。在下面的代码片段中查看:

$(function () {
  $("button").click(function () {
    $(this).addClass("btn-disabled").prop("disabled", true);
  });
});
body {padding: 50px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<button class="btn btn-primary">Disable Me</button>

关于javascript - 删除JQuery中的单个单词属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52191183/

相关文章:

javascript - 每次击球时将球的颜色更改为随机颜色

jQuery 自定义点击

javascript - 谷歌浏览器新内容行为

html - 使用 CSS3 Shapes 作为居中背景

javascript - Firefox 附加组件开发 |重写browser.xul

javascript - 将异步函数与同步函数混合?

javascript - php隐藏一个动态构造的元素

javascript - 是否有可能知道 jquery 可拖动元素将恢复到哪里?

javascript - 如何从 C3 JS 图表的选择器中获取对象引用

html - 如何在html中响应对齐内容?