简体   繁体   English

在jQuery中使用typeof运算符出现问题

[英]Trouble using typeof operator in jQuery

This will be the first time on SO I don't preface my question with, "I'm new to programming" (whoops). 这将是第一次,所以我不以“我是编程新手”(whoops)开头我的问题。 I'll get right into it: 我将直接进入它:

Here's my HTML code: 这是我的HTML代码:

<div class="hide" id="hide2">
    <div class="inputselect">   
    <p>
        <label>What is your budget?</label>
        </p>
        <p>
            Minimum: $<input type="text" id="minBud" name='minBud'><br />
            Maximum: $<input type="text" id="maxBud" name='maxBud'><br />
        <br />
        </p>
        <button type="button" class="next" id="next3">Next</button>
        <button type="button" class="prev" id="prev2">Previous</button> 
    </div>
</div>

Here's the jQuery/javascript code snippet I'm struggling with: 这是我苦苦挣扎的jQuery / javascript代码段:

$("#next3").click(function(){
    if (typeof $("#minBud").val() === "number" && typeof $("#maxBud").val() === "number") { 
        gunsPrice();
        $("#hide3").slideDown("fast");
        $("#hide2").slideUp("fast");
    } else {
        alert("Please only enter NUMBERS into the budget fields");
    }
});

What I'd like to see happen is: if the entries the the minBud and maxBud fields are numbers, run gunsPrice() and present the next question (HTML form). 我想看到的是:如果minBudmaxBud字段中的条目是数字,请运行gunsPrice()并显示下一个问题(HTML表单)。

I've tried altering the syntax several different ways, but no matter how I do it, numbers continue to trigger the alert . 我尝试以几种不同的方式更改语法,但是无论如何,数字都会继续触发alert

Note: I can get the typeof operator to function correctly within gunsPrice() (using variables gathered with getElementById , but that doesn't do me any good within this application; it always moves to the next question (due to the way I've set up the jQuery code). 注意:我可以使typeof运算符在gunsPrice()正确运行(使用与getElementById收集的变量,但这在此应用程序中没有任何好处;它总是移至下一个问题(由于我的方式设置jQuery代码)。

Thanks! 谢谢!

.val() always return a string for inputs. .val()始终返回输入字符串。 so you condition will always evaluate to false. 因此您的条件将始终为假。 You can however check to see if the results only contain digits(if that's what you're looking for) 但是,您可以检查结果是否仅包含数字(如果您要查找的是数字)

if (/^\d+$/.test($("#minBud").val()) && /^\d+$/.test($("#maxBud").val())) {

I now see that you can add hooks in jQuery so .val() can return a number. 我现在看到可以在jQuery中添加钩子,因此.val()可以返回数字。 You can hook into val() using the following code: 您可以使用以下代码挂接到val()中:

$.valHooks.text={
  get:function(elem){
    var i=parseFloat(elem.value,10);
    return (isNaN(i))?elem.value:i
  }
}

Now val() of any input type text will return a string or number depending if it can be converted. 现在,任何输入类型文本的val()都将返回字符串或数字,具体取决于是否可以转换。

This might be confusing though because the input type is TEXT. 但是,这可能会造成混淆,因为输入类型是TEXT。

You could use $valHooks.number and set the input of type number but tried this in Firefox 20.0.1 and didn't work. 您可以使用$ valHooks.number并设置数字类型的输入,但是在Firefox 20.0.1中尝试了此操作,但没有效果。

This because jQuery tries to get the hook using 这是因为jQuery尝试使用

jQuery.valHooks[ elem.type ] 

Since FireFox will return "text" when asking for an elem.type of type "number" it'll not call your hook and return the elem.value property wich is string. 由于FireFox在询问类型为“数字”的elem.type时将返回“文本”,因此它不会调用您的钩子并返回elem.value属性,该属性为string。 This is probably a bug in FireFox as 这可能是FireFox中的错误,因为

elem.getAttribute("type")

returns "number" 返回“数字”

A way to overcome this bug is to go into jQuery source code and change the following line: 解决该错误的一种方法是进入jQuery源代码并更改以下行:

hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ];

to

hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ]
  || jQuery.valHooks[ elem.getAttribute("data-type") ];

Now in your html you can specify a number input field by: 现在,在您的html中,您可以通过以下方式指定数字输入字段:

<input type="number" data-type="number" ...

And add a nubmer hook: 并添加一个nubmer钩子:

$.valHooks.number={...//see code above for the rest

In $("#minBud").val() , #minBud is an input or textarea. $("#minBud").val()#minBud是输入或文本区域。

Your operation: 您的操作:

typeof $("#minBud").val() 

is a "String". 是一个“字符串”。

parseInt($("#minBud").val(),10) === null 

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

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