0

I have an array of select dropdowns as such:

<select name="item[1]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[2]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[3]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[4]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>

I am trying to parse through them using Jquery. The number in the name is populated by the ID number of the item in my database. I basically want to parse through each one, if it is a yes, then use the ID to get the price of the item from the database to calculate a total. Is this even possible with Jquery? I can do the rest of the code no problem, I just need help with parsing through all of the item and return the value and ID for each one.

2
  • 1
    have you tried anything ? Commented Apr 6, 2015 at 17:29
  • $("select").each(function() { if (this.value == "Yes") //do stuff }) Commented Apr 6, 2015 at 17:30

3 Answers 3

1

I'd use the jQuery attribute starts with selector, and combine with .map() to get the list of your selected items.

Here's a runnable snippet that returns an array of selected items after clicking the button. It's up to you to take the item ids and generate your price.

$("button").on("click", function() {
  var ids = $('select[name^="item"]').map(function() {
    if ($(this).val() === "Yes")
      return this.name.replace(/\D/g,'');
    else 
      return null;
  }).toArray();
  
  alert(ids);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select name="item[1]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[2]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[3]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[4]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<button>Get Price</button>

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a full example (the getIds method is the part you want)

function getIds(){
  var selected = $('select[name^="item["]').filter(function(){
    return this.value == 'Yes';
  }).map(function(){
    return this.name.match(/\d+/);
  });
  return selected.get();
}

// just for display purposes
// you should call the getIds() method when you need to..
$('select').change(function(){
  var ids = getIds();
  $('.results').text( ids );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select name="item[1]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[2]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[3]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>
<select name="item[4]">
    <option value="No">No</option>
    <option value="Yes">Yes</option>
</select>

<div class="results"></div>

Comments

0

I haven't tested it, but you could try something like this:

$("select[name^='item']").each(function(){
console.log($(this).attr("name") + ": " + $(this).find(":selected").attr("value"));
});

With a slight tweak, you could turn that into key value pairs, and then, if I understand the question correctly, potentially use eval() to get the desired value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.