0

If my title seems stupid, forgive me. I'm a newbie in jQuery and I don't know to explain my problem using the correct jQuery terms.

So I asked this question a while ago and got the perfect answer. My problem now is that the value in my dropdownlist is not that simple and the solution given to me fails because of that.

The code given to me was $("select[id*='ComboBox'][value!=0]").length + offset;. In my sample code that's fine because the values are 0, 1, 2, 3. But in reality the values are like default_price1, code1_price2, code2_price3... So basically the values are concatenated strings. I know how to split strings but my problem now is how do I integrate that with my previous code?

$("select[id*='ComboBox'][value.split('_')[0]!==default]").length + offset; doesn't seem to work.

Is that even possible? Thanks again.

Here's the jsfiddle as requested: http://jsfiddle.net/annelagang/scxNp/20/

Community
  • 1
  • 1
Annie Lagang
  • 3,185
  • 1
  • 29
  • 36
  • Are you saying you want to count the number of select elements on the page that have a currently selected item with a value that doesn't start with the text "default"? – nnnnnn Jan 06 '12 at 04:27
  • can you set up a jsfiddle so we can see what you have so far? – mhps Jan 06 '12 at 04:28
  • Sounds like you need a regexp for your selector, take a look at http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions – leopic Jan 06 '12 at 04:31
  • Do you want to do sum of All select values like default_price1, code1_price2, code3_price3.. Or only what is selected? – Umesh Patil Jan 06 '12 at 04:51

3 Answers3

1

Don't abuse jQuery selectors, better use the filter method.

$("select[id*='ComboBox']")
   .filter(function(){return this.value.replace(/_.*/,"") !== "default"}).length
   + offset;
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Ivan Castellanos
  • 8,041
  • 1
  • 47
  • 42
1

If the following works for value of "0":

$("select[id*='ComboBox'][value!=0]").length + offset

Then for values beginning with "default" you should be able to do something like this:

$("select[id*='ComboBox']").not("[value^='default']").length + offset

Where ^= is "starts with".

But you could use the .filter() method, which would allow you to do whatever kind of string manipulation you like to test whether each item should be included:

$("select[id*='ComboBox']").filter(function() {
   return /^default/.test($(this).val());
}).length + offset

/^default/ is a regex that looks for "default" at the beginning of the string.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Nice. This really means that I really really need to study jQuery more seriously now. Thanks! – Annie Lagang Jan 06 '12 at 04:37
  • This doesn't include into sum of selects if the value starts with 'code1_'. @AnneLagang, Is this what you expected ? – Umesh Patil Jan 06 '12 at 04:59
  • @Umesh - This question _was_ unclear, but after looking at the question she linked to about counting select elements with a non-zero value selected I took a guess that essentially she wants to check how many select elements are not still on the default value. – nnnnnn Jan 06 '12 at 05:25
  • @nnnnnn, Anyway, She accepted the answer means she got what she wanted from your answer :) – Umesh Patil Jan 06 '12 at 05:45
0
$(function() {
    $("select[id*='ComboBox']").change(function() {
        var sum=0;
        $(this).find("option").each(function(){
          sum+=parseInt($(this).val().split("price")[1]);        
        });     
        $("#total").text(sum);
    });
});

If user selects any value from dropdown. Sum is dispalyed (1+2+3+4)=10

You can use string spliting logic in your way. but , Is it what you wanted ?

Umesh Patil
  • 10,475
  • 16
  • 52
  • 80