English 中文(简体)
与 jQuery 比较
原标题:compare with jQuery
  • 时间:2012-05-28 13:00:07
  •  标签:
  • jquery

我有一个阵列叫做 billemits 在我的jquery, 并且有这样的数值,

[TUTION FEE ,SPORTS FEE ,CONVOCATION FEE ]

在添加新项目之前,在切换 billemits 之前,在切换 billemts ,以避免重复,例如

for (var i = 0; i < billItems.length; i++) {
    var name = billItems[i];

    if (name == feesItem.ItemName) {  //here ItemName=TUTION FEE
        var validItem =  False 
    }
}

如上文所述,我正在通过 ItemName 作为 TUTION FEE , 但它没有比较字符串和控件,即使其内容相同,也无法进入区块。

正在将数据附加到 html 表格中

var feesItem = new Object();

  feesItem.ItemName     = $("#txtItemName").val();
  feesItem.Amount       = $("#txtAmount").val();

$("#BillTable > tbody").find("tr:gt(0)").remove();
  for (var i = 0; i < feesItemList.length; i++) {  
tab = "<tr><td>" + " <span class= bid >" + feesItem[i].ItemName + " </span>
       </td><td>" + feesItem[i].Amount + "</td></tr>";

          $("#BillTable > tbody").append(tab);
}

它仍然没有比较这些数值...

我只是试着比较一下圆圈里的价值

 var billItems = [];
  $(".bid").each(function () {
      billItems.push($(this).text());
  });

 var validItem = ($.inArray(feesItem[i].ItemName, billItems) < 0);
最佳回答

我将四肢而下 猜测你的字符串和看上去的不完全一样

if ($.trim(name) == $.trim(feesItem.ItemName)) {

这将删除两个参数中的任何后遗空或前导空格, 比较之前。 希望有帮助 。 @ info: whatsthis

问题回答

使用 < a href=> "http://api.jquery.com/jQuery.inArray/" rel="nofollow"\\\code>$.inArray () :

var billItems = [ TUITION FEE ,  SPORTS FEE ,  CONVOCATION FEE ];
var validItem = ($.inArray(ItemName, billItems) < 0);

您可以为此使用 jQuery S 的阵列函数 :

Here s the documentation: http://api.jquery.com/jQuery.inArray/

并在此设置一些代码, 以演示您如何使用它 :

var myArray = [ TUTION FEE  , SPORTS FEE  , CONVOCATION FEE  ];
var itemName =  SPORTS FEE ;
var nonExistentItemName =  OTHER FEE ;
var isValid = false;

if($.inArray(itemName, myArray) == -1){
    isValid = true;
}

alert(isValid);

isValid = false;

if($.inArray(nonExistentItemName, myArray) == -1){
    isValid = true;
}

alert(isValid);

and here is the jsFiddle to demonstrate it: http://jsfiddle.net/sPeem/

注意 @ Rory McCrossan 有关Var validItem = 假的评论@ Rory McCrossan

还要确保您比较字符串与字符串或 Int vs int javascript 在比较数值类型时,可以给出一些假的 vales 。

改写你的代码的方法是

//Search for a specified value within an array and return its index (or -1 if not found).
var index = jQuery.inArray(ItemName, billItems);

var validItem = index > -1;

尝试此 :

validItem = true;

for(var i = 0; i < billItems.length; i++) {
    var name = billItems[i];

    if(name == ItemName) { 
       validItem = false;
       break;
    }
}

if(validItem){
  billItems.push(ItemName);
}




相关问题
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: &...