English 中文(简体)
Jquery:在某种情况下,以任何方式写短的几处比较?
原标题:Jquery: several comparations inside an if, any way to write it shorter?

a)

 if($( #address_nations_id ).val() != 105 && $( #address_nations_id ).val() != 74)

是否有办法缩短这一期限?

我试图这样做:

if($( #address_nations_id ).val() != 105 && != 74)

但是,它努力工作。

任何想法?

关于

Javier

问题回答

自2006年以来 I(基于ID),你希望检查<>m>many,更多人可使用>:$.inArray(,方便地扩大该名单,例如:

if($.inArray($( #address_nations_id ).val(), ["105", "74"]) > -1)

http://jsfiddle.net/nick_craver/hfgxE/"rel=“nofollow> 您可以在此请见,并铭记您对这种方法的需要说明,因为 Java成文的打字与“<条码>=这里的打字,以及.val( > 回归指示。

你们可以做的一件事是事先获得价值:

var val = $( #address_nations_id ).val();
if(val != 105 && val != 74)

这比你只使用一种方法,而不是两种方法更重要。

如果你们有许多价值观,我就提出这样的一份研究表:

var values = {
    "105": 1,
    "74": 1
}

并且

if(!($( #address_nations_id ).val() in values)) {
    //...
}

这应当比寻找阵列的价值更快。

var val = $( #address_nations_id ).val();

if(val !=  105  && val !=  74 ) { 
  //do stuff
}

http://www.ohchr.org。

d 查阅Felix s recommendation。 但只是为了完全:

在有<代码>indexOf的浏览器上,您可以:

if ([105, 74].indexOf(parseInt($( #address_nations_id ).val())) >= 0)

......但我只建议两个价值观。 它不太清楚、不必要地复杂,并非所有浏览者都有。 这些批评大多也适用于使用jQuery sin Array功能。

也可在<条码>上填写。

switch (parseInt($( #address_nations_id ).val())) {
    case 105:
    case 74:
        break;
    default:
        /* do your thing here *?
        break;
}

......这几乎不等于是短的,但几乎肯定是适合两个价值观的。





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

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.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签