jeudi 21 mai 2015

How to find out if a Javascript variable has certain characters in it

I am very new to javascript so please bare with me,

We have a quick order form which take in the product code (sku) and returns the product and enters a quantity of 1.

What we want to happen is any SKU that starts with EN9 we want the Quantity to be set to 0.

I have tried to work this out for myself but seem to be going no where as i have very minimum javascript experience.

This is the code which set everything

   var _parent = $j(this).parent().parent().parent();
    var _productid = $j(this).find('.prodid').val();
    var _sku = $j(this).find('.sku').html();
    var _name = $j(this).find('.name').html();
    var _price = $j(this).find('.pprice').val();
    var _temp_name = _name;

    _temp_name = _temp_name.replace(/(<([^>]+)>)/ig,"");

    _parent.find('.txt-sku').html(_sku);
    _parent.find('.txt-pcode').val(_temp_name);
    _parent.find('.txt-qty').val(1);
    _parent.find('.pname').html(_name);
    _parent.find('.pqty').html(_price);
    _parent.find('.btn-addcart').attr('alt', _productid);
    $j('#allid').val($j('#allid').val()+','+_productid);
    $j(this).parent().hide();
    _key = '';

I can see _parent.find('.txt-qty').val(1); sets the qty to 1 so i assumed i would have to wrap an IF statement around this.

I tried

    if ( _sku == ('EN9)){
        _parent.find('.txt-qty').val(1);}
else{
    _parent.find('.txt-qty').val(0);
}

But with no luck. SO i change -sku to .sku but still no luck. If anyone could help it would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire