简体   繁体   English

css / javascript元素在应返回true时返回false

[英]css/javascript element returning false when it should return true

I have a one field form (text input and submit button). 我有一个字段表单(文本输入和提交按钮)。 Here is the form code: 这是表单代码:

<form id="new_skill" class="new_skill" method="post" action="/skills" >
    <li>
        <input id="resume-field" class="field field288" type="text"
          value="Type a speciality you want to add to your profile" 
          title="Type a speciality you want to add to your profile" 
          name="skill[label]"></input>
    </li>
    <li class="td80">
        <input class="button button-add button-add-disabled" 
         type="submit" value="ADD +" name="commit"></input>
    </li>
</form>

Using javascript, if text is entered in the text field, the submit button should be unclickable. 使用javascript,如果在文本字段中输入了文本,则提交按钮应该不可单击。 If there is no text in the field, it should be clickable. 如果该字段中没有文本,则应单击该文本。 I am doing that by using javascript to remove and/or put back the button-add-disabled class. 我这样做是通过使用javascript删除和/或放回禁用按钮添加的类。 Here is the javascript: 这是JavaScript:

(function($){
    $(document).on('focusin', '#resume-field', function() {
        $(this).parents().find('.button-add-disabled').removeClass('button-add-disabled');
    }).on('focusout', '#resume-field', function(){
        if(this.value==' '||this.title==this.value) {
            $(this).parents().find('.button-add').addClass('button-add-disabled');
        } else {
            $(this).parents().find('.button-add').removeClass('button-add-disabled');
        }

    });     

    $('.button-add-disabled').click(function(){
        return false;
    });
}(jQuery));

And here is the css: 这是CSS:

.button-add { width: 49px; height: 28px; border: solid 1px #8c8c8c; display: block; 
   font-size: 11px; line-height: 28px ; color: #fff; text-align: center; 
   font-family: 'Ubuntu', sans-serif; transition: none; margin: 0 0 0 auto; 
   border-radius: 3px; }
.button-add:hover { text-decoration: none;
   -webkit-transition:none; 
      -moz-transition:none; 
       -ms-transition:none; 
        -o-transition:none; 
           transition:none; 
}
.td80 .button-add { margin-left:35px !important;  }
.button-add-disabled { background: url(/assets/add-specialities-disabled.png) 
   repeat-x 0 0; box-shadow: 0 0 0 0; margin-left:35px;  }
.button-add-disabled:hover { background: url(/assets/add-specialities-disabled.png) 
   repeat-x 0 0; box-shadow: 0 0 0 0;  }

The classes are changing as expected and the javascript is working. 该类正在按预期方式更改,并且javascript正在运行。 For some reason though, even if .button-add-disabled is not applied to the form element, the form element is still returning false and therefore won't submit. 但是由于某种原因,即使未将.button-add-disabled应用于form元素,form元素仍然返回false,因此不会提交。 When "button-add-disabled" is removed by the javascript, the form should submit. 当javascript删除了“ button-add-disabled”后,该表单应提交。 I can see the server logs. 我可以看到服务器日志。 If I remove the line from the javascript "return: false", the form works, So i know the form itself works. 如果我从javascript“ return:false”中删除该行,则该表单有效,所以我知道该表单本身有效。 I'm pretty sure something is wrong with the javascript. 我很确定JavaScript出了点问题。 Any ideas? 有任何想法吗?

That's not how that works. 那不是那样的。 Events are bound to elements, which are reached via selectors; 事件绑定到元素,这些元素通过选择器到达; they are not bound to selectors. 它们未绑定到选择器。

When you bind the event directly to the element, the event is now bound to that element until you explicitly unbind it. 当您直接将事件绑定到元素时,事件现在将绑定到该元素,直到您明确取消绑定为止。 The original selector is no longer relevant. 原始选择器不再相关。

You need to do this, or something like it: 您需要执行此操作或类似的操作:

$('.button-add-disabled').click(function(){
  return !$(this).hasClass('button-add-disabled');
});

That is, test whether the button is currently disabled by your class at the point the event is raised. 也就是说,在引发事件时测试您的班级当前是否禁用了该按钮。

As an aside, this... 顺便说一句,这...

if(this.value==' '||this.title==this.value) {
  $(this).parents().find('.button-add').addClass('button-add-disabled');
} else {
  $(this).parents().find('.button-add').removeClass('button-add-disabled');
}

should be this: 应该是这样的:

var disabled = this.value == ' ' || this.title == this.value;
$(this).parents().find('.button-add').toggleClass('button-add-disabled', disabled);

You want to set/remove the disabled attribute of the input element, not set a CSS style which is for display purposes only 您要设置/删除输入元素的禁用属性 ,而不是设置仅用于显示目的的CSS样式

$('#resume-field').on('change', function() {
    if ($(this).val().length == 0) {
        $(this.form).find('input[type=submit]').attr('disabled', false).removeClass('button-add-disabled');
    } else {
        $(this.form).find('input[type=submit]').attr('disabled', true).addClass('button-add-disabled');
    }
})

jsFiddle Demo jsFiddle演示

Also be sure that you handle the submission of the form when the user presses enter in the input field, you can do that using the jQuery .submit event handler and preventing the default behaviour. 另外,请确保在用户在输入字段中按下Enter键时处理表单的提交,您可以使用jQuery .submit事件处理程序并防止默认行为来进行提交 It is also essential you handle this server side. 处理此服务器端也很重要。

EDIT: I just noticed what the CSS was doing, updated answer. 编辑:我只是注意到CSS在做什么,更新的答案。

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

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