Hier ist meine Javascript-Funktion, wo ich zwei Strings vergleichen einen Wert erhalten als Funktionsparameter und ein anderes holt von JSON, die von Frühjahr Antwort kommt.Vergleichen Sie zwei Strings funktioniert nicht richtig in Javascript
Aber ich bin sehr überrascht Ich bin nicht in der Lage, zwei Zeichenfolgen mit ===
und ==
zu vergleichen, kann ich die Werte im Debugger sehen.
ich meine Funktion von meinem HTML-Code caling:
<textarea onblur="searchProduct(this.value)"></textarea >
Javascript:
function searchProduct(value)
{
alert(value);
for (i in allProducts) {
console.log(allProducts[0].productName);
var tempProductName=allProducts[i].productName;
alert(tempProductName.toString() == value.toString());
// checks product name is present or not
if(tempProductName.toString() === value.toString()){
// setting other values details of products
$('#description').val(allProducts[i].productDescription);
$('.cost').val(allProducts[i].unitPrice);
}//if
}
}//searchProduct
allProducts: Antwort vom Frühjahr
[{ "productId": 3, "sku": "QTY", "vendorProductId": "v1", "productName": "P1name", "productDescription": "p1 desc", "supplierId": "s1", "categoryId": "c", "mengePerUnit": 1,0, "unitPrice": 760,0, "msrp": 1,0, "availableSize": 40,0, "availableColors": "schwarz, blau", "size": 40.0, "Farbe": "weiß", "Rabatt": 0.0, "unitWeight": 1.0, "unitsInStock": 148.0, "unitsOnOrder": 500.0, "reorderLevel": 150.0, "productAvailable": "Y", "discountAvailable ":" N "," currentOrder ": 0.0," BildURL ":" Bildpfad "," Ranking ": 10," Notiz ":" Beispielnotizen "," SMS-Notifikation ":" N "," productExpiryDate ": null, "productDeleted": null, "alertExpiryDate": null, "idsku": "IDSKU1"}, {"productId": 4, "sku": "QTY", "vendorProductId": "v1", "productName": "P2name "productDescription": "p1 desc", "supplierId": "s1", "categoryId": "c", "mengePerUnit": 1,0, "unitPrice": 1,0, "msrp": 1,0, "availableSize": 40,0 , "availableColors": "schwarz, blau", "Größe": 40,0, "Farbe": "weiß", "Rabatt": 0,0, "unitWeight": 1,0, "unitsInStock": 150,0, "unitsOnOrder": 500,0, " reorderLevel ": 150.0," productAvailable ":" Y "," discountAvailable ":" N "," currentOrder ": 0.0," BildURL ":" Bildpfad "," Ranking ": 10," note ":" Sample Notes " , "smsNotification": "N" "productExpiryDate": null, "productDeleted": null, "alertExpiryDate": null, "idsku": "IDSKU1"}, {"productId": 5, "sku": "QTY", "vendorProductId": "v1" "productName": "P3name", "productDescription": "p1 desc", "supplierId": "s1", "categoryId": "c", "mengePerUnit": 1.0, "unitPrice": 1.0, "msrp": 1.0, "availableSize": 40.0, "availableColors": "schwarz, blau", "size": 40.0, "color": "weiß", "discount": 0.0, "unitWeight": 1.0, "unitsInStock": 1000.0, "unitsOnOrder": 500.0, "reorderLevel": 150.0, "productAvailable": "Y", "discountAvailable": "N", "currentOrder": 0.0, "BildURL": "Bildpfad", "Ranking": 10, " note ":" Beispielnotizen "," smsNotification ":" N "," productExpiryDate ": null," productDeleted ": null," alertExpiryDate ": null," idsku ":" IDSKU1 "}, {" productId ": 6, "sku": "QTY", "vendorProductId": "v1", "productName": "P4name", "productDescription": "p1 desc", "supplierId": "s1", "categoryId": "c", " mengePerUnit ": 1.0," unitPrice ": 1.0," msrp ": 1.0," availableSize ": 40.0," availableColors ":" schwarz, blau "," size ": 40.0," color ":" weiß "," discount " : 0.0, "unitWeight": 1.0, "unitsIn Stock ": 1000.0," unitsOnOrder ": 500.0," reorderLevel ": 150.0," productAvailable ":" Y "," discountAvailable ":" N "," currentOrder ": 0.0," BildURL ":" Bildpfad "," Ranking ": 10," Notiz ":" Beispielnotizen "," SMS-Benachrichtigung ":" N "," ProductExpiryDate ": null," ProductDeleted ": null," AlertExpiryDate ": null," Idsku ":" IDSKU1 "}, {" productId ": 7," sku ":" QTY "," vendorProductId ":" v1 "," productName ":" P5name "," productDescription ":" p1 desc "," supplierId ":" s1 "," categoryId ": "c", "mengePerUnit": 1,0, "unitPrice": 1,0, "msrp": 1,0, "availableSize": 40,0, "availableColors": "schwarz, blau", "size": 40,0, "color": "weiß "," Rabatt ": 0.0," unitWeight ": 1.0," unitsInStock ": 1000.0," unitsOnOrder ": 500.0," reorderLevel ": 150.0," productAvailable ":" Y "," discountAvailable ":" N "," currentOrder ": 0.0," pictureURL ":" Bildpfad "," Rangfolge ": 10," Notiz ":" Beispielnotizen "," smsNotification ":" N "," productExpiryDate ": null," productDeleted ": null," alertExpiryDate ": null," idsku ":" IDSKU1 "}, {" productId ": 8," sku ":" QTY "," HerstellerProduktId ":" v1 "," productName ":" P6name "," productDescription ":" p1 beschreibung "," supplierId ":" s1 "," KategorieId ":" c "," mengePerUnit ": 1.0," unitPrice ": 1.0," msrp ": 1.0," availableSize ": 40.0," availableColors ":" schwarz, blau "," Größe ": 40.0," Farbe ":" weiß "," Rabatt ": 0.0," unitWeight ": 1.0," unitsInStock ": 1000.0," unitsOnOrder ": 500.0," reorderLevel ": 150.0," productAvailable ": "Y", "RabattAvailable": "N", "CurrentOrder": 0.0, "pictureURL": "Bildpfad", "Ranking": 10, "note": "Beispielnotizen", "smsNotification": "N", "productExpiryDate": null, "productDeleted": null, "alertExpiryDate": null, "idsku": "IDSKU1"}, {"productId": 9, "sku": "QTY", "vendorProductId": "v1", "productName": "P7name", "productDescription": "p1 desc" "supplierId": "s1", "categoryId": "c", "mengePerUnit": 1.0, "unitPrice": 1.0, "msrp": 1.0, "availableSize": 40.0, "availableColors": "schwarz, blau" , "Größe": 40,0, "Farbe": "weiß", "Rabatt": 0,0, "unitWeight": 1,0, "unitsInStock": 1000,0, "unitsOnOrder": 500,0, "reorderLevel": 150,0, "productAvailable": " Y "," RabattAvailable ":" N "," CurrentOrder ": 0.0," BildURL ":" Bildpfad "," Ranking ": 10," Notiz ":" Sample Notes "," SMSNotification ":" N "," productExpiryDate ": null," productDeleted ": null," alertExpiryDate ": null," idsku ":" IDSKU1 "}, {" productId ": 10," sku ":" QTY "," vendorProductId ":" v1 "," productName ":" P8name "," productDescription ":" p1 desc "," Lieferanten-ID ":" s1 "," categoryId ":" c "," mengePerUnit ": 1.0," unitPrice ": 1.0," msrp ": 1.0, "availableSize": 40.0, "availableCo lors ":" schwarz, blau "," Größe ": 40.0," Farbe ":" weiß "," Rabatt ": 0.0," unitWeight ": 1.0," unitsInStock ": 1000.0," unitsOnOrder ": 500.0," reorderLevel " : 150.0, "productAvailable": "Y", "discountAvailable": "N", "currentOrder": 0.0, "BildURL": "Bildpfad", "Ranking": 10, "note": "Beispielnotizen", " "smsNotification": "N", "productExpiryDate": null, "productDeleted": null, "alertExpiryDate": null, "idsku": "IDSKU1"}, {"productId": 11, "sku": "QTY", " vendorProductId ":" v1 "," productName ":" P9name "," productDescription ":" p1 desc "," Lieferanten-ID ":" s1 "," categoryId ":" c "," mengePerUnit ": 1.0," unitPrice ": 1.0, "msrp": 1.0, "availableSize": 40.0, "availableColors": "schwarz, blau", "Größe": 40.0, "color": "weiß", "Rabatt": 0.0, "unitWeight": 1.0, "unitsInStock": 1000.0, "unitsOnOrder": 500.0, "reorderLevel": 150.0, "productAvailable": "Y", "discountAvailable": "N", "currentOrder": 0.0, "pictureURL": "Bildpfad", " ranking ": 10," note ":" Beispielnotizen "," smsNotification ":" N "," productExpiryDate ": null," productDeleted ": null," alertExpiryDate ": null," idsku ":" IDSKU1 "}, { "Produkt-ID": 1 2, "sku": "QTY", "vendorProductId": "v1", "productName": "P9name", "productDescription": "p1 desc", "supplierId": "s1", "categoryId": "c" , "mengePerUnit": 1,0, "unitPrice": 1,0, "msrp": 1,0, "availableSize": 40,0, "availableColors": "schwarz, blau", "size": 40,0, "color": "weiß", " Rabatt ": 0.0," unitWeight ": 1.0," unitsInStock ": 1000.0," unitsOnOrder ": 500.0," reorderLevel ": 150.0," productAvailable ":" Y "," discountAvailable ":" N "," currentOrder ": 0.0 , "pictureURL": "Bildpfad", "Rangfolge": 10, "Notiz": "Beispielnotizen", "smsNotification": "N", "productExpiryDate": null, "productDeleted": null, "alertExpiryDate": null , "idsku": "IDSKU1"}, {"productId": 13, "sku": "QTY", "vendorProductId": "v1", "productName": "P9name", "productDescription": "p1 desc", "supplierId": "s1", "KategorieId": "c", "mengePerUnit": 1.0, "unitPrice": 1.0, "msrp": 1.0, "availableSize": 40.0, "availableColors": "schwarz, blau", "Größe": 40.0, "Farbe": "Weiß", "Rabatt": 0.0, "Einheitsgewicht": 1.0, "MaßeinheitenInStock": 1000.0, "EinheitenAuftrag": 500.0, "ReorderLevel": 150.0, "ProduktAvailable": "Y "," RabattVerfügbar ":" N "," currentOrder ": 0.0," pictureURL ":" Bildpfad "," Ranking ": 10," note ":" Beispielnotizen "," smsNotification ":" N "," productExpiryDate ": null," productDeleted ": null, "alertExpiryDate": null, "idsku": "IDSKU1"}, {"productId": 14, "sku": "QTY", "vendorProductId": "v1", "productName": "P9name", "productDescription ":" p1 desc "," supplierId ":" s1 "," KategorieId ":" c "," mengePerUnit ": 1,0," unitPrice ": 1,0," msrp ": 1,0," availableSize ": 40,0," availableColors " : "schwarz, blau", "Größe": 40,0, "Farbe": "weiß", "Rabatt": 0,0, "unitWeight": 1,0, "unitsInStock": 1000,0, "unitsOnOrder": 500,0, "reorderLevel": 150,0 , "productAvailable": "Y", "discountAvailable": "N", "currentOrder": 0.0, "BildURL": "Bildpfad", "Ranking": 10, "note": "Beispielnotizen", "smsNotification" : "N", "productExpiryDate": null, "productDeleted": null, "alertExpiryDate": null, "idsku": "IDSKU1"}, {"productId": 15, "sku": "QTY", "vendorProductId" : "v1", "productName": "P9name", "productDescription": "p1 desc", "Lieferanten-ID": "s1", "categoryId": "c", "mengePerUnit": 1.0, "unitPrice": 1.0, "msrp": 1.0, "availableSiz e ": 40.0," availableColors ":" schwarz, blau "," size ": 40.0," color ":" weiß "," discount ": 0.0," unitWeight ": 1.0," unitsInStock ": 1000.0," unitsOnOrder " : 500.0, "reorderLevel": 150.0, "productAvailable": "Y", "discountAvailable": "N", "currentOrder": 0.0, "BildURL": "Bildpfad", "Ranking": 10, "Hinweis": "Beispielnotizen", "smsNotification": "N", "productExpiryDate": null, "productDeleted": null, "alertExpiryDate": null, "idsku": "IDSKU1"}, {"productId": 16, "sku" : "QTY", "vendorProductId": "v1", "productName": "P10name", "productDescription": "p1 desc", "supplierId": "s1", "categoryId": "c", "mengePerUnit": 1,0, "unitPrice": 1,0, "msrp": 1,0, "availableSize": 40,0, "availableColors": "schwarz, blau", "Größe": 40,0, "color": "weiß", "Rabatt": 0,0, "unitWeight": 1.0, "unitsInStock": 1000.0, "unitsOnOrder": 500.0, "reorderLevel": 150.0, "productAvailable": "Y", "discountAvailable": "N", "currentOrder": 0.0, "pictureURL": "Bildpfad", "Ranking": 10, "note": "Beispielnotizen", "smsNotification": "N", "productExpiryDate": null, "productDeleted": null, "alertExpiryDate": null, "idsku":
String.prototype.equals = function (str2) { return this.indexOf(str2) == 0 && this.length == str2.length; }
Oder ein "PHP" Stil wie Methode: "IDSKU1"}]
Warum nicht verwenden [ 'indexOf'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/ Referenz/Globale_Objekte/String/indexOf)? – hindmost
@hindmost, ich möchte es vergleichen, könnten Sie bitte Ihre Antwort posten –
Ich habe Ihren Code in meiner js-Konsole auf diese Weise ausgeführt: searchProduct ("P1name"); Das erste Ergebnis war "wahr". Bitte können Sie eine bessere Beschreibung für Ihr Problem schreiben? – Nico