1

I have a php script which display some dynamic select. I would like to get value from each select in jQuery script. An idea to help me?

PHP

$result = mysql_query("select * from $table where id='$id'");
$r = mysql_fetch_array($result);
$result[1]=$r['result_1'];
$result[2]=$r['result_2'];
$result[3]=$r['result_3'];
$result[4]=$r['result_4'];
$result[5]=$r['result_5'];

for($i=1;$i<=5;$i++)
{
    if($result[$i]!="0")
    {
     echo '<select id="myresult_$i"><option value="$result[$i]">$result[$i]</option></select>';
    }
}

echo '<div class="addmyprod" id="addprod_'.$id.'" idprod="'.$id.'">ADD</div>';

jQuery

$(".addmyprod").on('click', function() 
{
    var idprod= $('#addprod_'+$(this).attr('idprod')).attr('idprod');
    console.log(idprod);

    var myresult_1= ?????
    console.log(myresult_1);

    var myresult_2= ?????
    console.log(myresult_2);

    var myresult_3= ?????
    console.log(myresult_3);

    var myresult_4= ?????
    console.log(myresult_4);

    var myresult_5= ?????
    console.log(myresult_5);
            
}); 
2
  • you currently are echoing 5 selects with 5 differents ids with 1 option only for every single select. Are you aware of that? The logic of that is broken and should be changed. Commented Jun 16, 2015 at 9:24
  • Do you also need help on PHP? I see lot of answers on jQuery but it seems you don't have a correct ‌<select> generation. I suggest you to add HTML source if you don't want PHP answers. Besides, if your PHP only generates this particular tag, maybe it's necessary to have your ajax code. Commented Jun 16, 2015 at 9:57

4 Answers 4

2

Just add custom class to your select tag:-

for($i=1;$i<=5;$i++)
{
    if($result[$i]!="0")
    {
     echo '<select class="wdm_select" id="myresult_$i"><option value="$result[$i]">$result[$i]</option></select>';
    }
}

In js, put below code:-

$(".addmyprod").on('click', function() {
    $('.wdm_select').each(function() {
        console.log($(this).val());
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0
var myOpts = document.getElementById('yourselect').options;
my_result1 = myOpts[1];
my_result2 = myOpts[2];
etc...

from How to get all options of a select using jQuery?

easy to find with google....

Comments

0

I don't really understand your situation here. How does your code supposed to work in the first place ? What does the ADD div stands for ?

There's a way to get all selects values, by using a query like the one of Anik, if all of your selects possess the same kind of ID (supposed to be myresult_X):

$('select[id^="myresult_"]').each(function() {
  console.log($(this).val());
});

Here the selector ^= stands for "attribute starts with", according to JQuery API doc.

Comments

0

Build your html like and add class 'selectField'. Also as you are using select you need to add element selected condition into your script.

for($i=1;$i<=5;$i++)
{
    if($result[$i]!="0")
    {
     echo '<select class="selectField" id="myresult_$i"><option value="$result[$i]">$result[$i]</option></select>';
    }
}

An your js will be as:

$(document).ready(function(){
  $('select[class="selectField"]').each(function(index,item){
    var id=$(item).attr('id');
    alert($("#"+id+" option:selected").val()); 
  });
});

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.