English 中文(简体)
Jquery .val() = ** 添加多种可能的答复**
原标题:Jquery .val() == ** adding multiple possible answers **

我这样做是为了做到以下几点,但作为一个新的堡垒,我想知道,增加多种可能的价值是否有任何缩短。

if ($("input:first").val().toUpperCase() == "UNITED STATES" || $("input:first").val().toUpperCase() == "USA" || $("input:first").val().toUpperCase() == "AMERICA") {
                $("#check").text("Correct!").show().fadeOut(5000);
                return true;

if ($("input:first").val().toUpperCase() == ("UNITED STATES","USA","AMERICA")) {
                $("#check").text("Correct!").show().fadeOut(5000);
                return true;

仅此证明AMERICA一案的最后答案是正确的。

最佳回答

Using $.inArray(:

if( $.inArray( $("input:first").val().toUpperCase(), [ "UNITED STATES", "USA", "AMERICA" ] ) > -1 ) { ...

Demo: http://jsfiddle.net/4ar2G/

问题回答

在关键值和<代码>操作者:

var matches = {  UNITED STATES : 1,  USA : 1,  AMERICA : 1 };

if ($("input:first").val().toUpperCase() in matches) {
   ...
}

根据我的情况,这在实际中具有非常高的效率和效力, Java在研究物件的本质方面非常有帮助,而 for在大的组子上则不使用“准则”“O(n)

Don t use if you ve messed with Object.prototype, though!

为了平衡起见,等同的非金字塔(因为金字不当然意味着更好):

if(["UNITED STATES", "USA", "AMERICA"].indexOf($( input:first ).val().toUpperCase()) > -1) {
    $("#check").text("Correct!").show().fadeOut(5000);
    return true;
}

引证:

if($.inArray($("input:first").val().toUpperCase(), ["UNITED STATES","USA","AMERICA"]) > -1){
            $("#check").text("Correct!").show().fadeOut(5000);
            return true;
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签