简体   繁体   English

Attr确实可以,但是removeAttr似乎对我的按钮不起作用

[英]Attr does work, but removeAttr doesn't seem to work on my button

I want to create my button in such way that it disables if the user hovers over it, but the input fields were to be empty. 我想以这样的方式创建我的按钮:如果用户将鼠标悬停在该按钮上,该按钮将被禁用,但是输入字段将为空。 This works, however when I fill out everything and hover over the button again it remains disabled. 这行得通,但是当我填写所有内容并再次将鼠标悬停在按钮上时,它将保持禁用状态。 I looked up this link: Remove disabled attribute using JQuery? 我查找了此链接: 使用JQuery删除禁用的属性? and did what was said there (multiple answers tried), but none of them worked. 并按照那里所说的做了(尝试了多个答案),但是没有一个起作用。 I know the variables are correct, I already console.log(); 我知道变量是正确的,我已经console.log(); those. 那些。 So basically the question: What am I doing wrong or missing out on? 所以基本上是这样的问题:我在做错什么还是错过了?

The code: 编码:

function submitBtn(tracker) {
    let btn = ($('<button/>', {
        'type': 'submit',
        'class': 'btn btn-primary',
        'id': 'submitDataBtn',
        'name': 'submitDataBtn',
        'value': 'submit'
    })).text('Submit').hover(function() {
        let datePicker = $('#datePicker').val();
        let weight = $('#weight').val();
        let distance = $('#distance').val();
        let timePicker = $('#timePicker').val();

        if(datePicker === '' || weight === '' || distance === '' || timePicker === '') {
            $(this).attr('disabled', true);

        }else if (datePicker !== '' || weight !== '' || distance !== '' || timePicker !== '') {
            $(this).prop('disabled', false);

        }else {
            // do something else
        }   
    });
    return btn;
}

Disabled button cannot have hover event. 禁用按钮不能发生悬停事件。 If you wish to show it as disabled use class with opacity. 如果您希望将其显示为禁用,请使用不透明的类。 If you want to stop submission of the form - use validation on form, like: 如果要停止提交表单,请在表单上使用验证,例如:

document.getElementsByTagName("form")[0]).onsubmit=function() {
    return validate(this);
  }

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

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