简体   繁体   English

js.erb文件中的比较运算符

[英]comparison operators in js.erb files

I have a file called new.js.erb that contains this line: 我有一个名为new.js.erb的文件,其中包含以下行:

if( $('#user_password').val().length < 6 )

For some reason, the less-than symbol breaks down the whole file. 由于某些原因,小于号会分解整个文件。 I think Rails is seeing the < symbol as the beginning of a tag for some reason. 我认为出于某种原因,Rails将<符号视为标签的开头。 I just want to check if the value in the user_password field has a string length less than 6. Is there a way to do this in jQuery in a js.erb file? 我只想检查user_password字段中的值是否具有小于6的字符串长度。是否可以在js.erb文件的jQuery中做到这一点?

Amazingly, all my Googling and SO searching have not uncovered a solution. 令人惊讶的是,我所有的Google搜索和SO搜索都没有找到解决方案。

If you need any more information, please don't hesitate to ask for it. 如果您需要更多信息,请不要犹豫。

Thank you for any help! 感谢您的任何帮助!

More code for mu (I can guarantee everything above and below this block is working fine): 更多用于mu的代码(我可以保证此块上方和下方的所有内容都可以正常工作):

        if( $('#user_password').text().length < 6 )
        {
            $('form.edit_user').submit();
            popup.unload();
            $('#send_after_pass').show();
            $('#send_after_pass').click(function(e) {
                $(this).hide();
                $('form.new').submit();
            });
        else
        {
            $('#errors').html("Password must be 6 characters or longer.");
        }

EDIT: My bad on the bogus quote! 编辑:我对虚假报价不好! Pretend it was never there... That isn't what caused my problem, though. 假装它从来没有出现过……那不是造成我问题的原因。

I don't see any ERB in the code you posted but there is a missing closing brace, your code should look more like this: 我在您发布的代码中没有看到任何ERB,但是缺少右括号,您的代码应更像这样:

    if( $('#user_password').text().length < 6 )
    {
        //...
    }
    else
    {
        $('#errors').html("Password must be 6 characters or longer.");
    }

Note the closing brace I added above your else . 请注意,我在您的else上方加上了右括号。

为什么不尝试像这样逆转条件,看看它是否确实是操作员?

if (6 > $('#user_password').val().length)

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

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