views:

37

answers:

1

this is a nutts job here. Not sure if jquery can handle this. I have a php page called page1.php where I have drop down like this

<select id="startRecord" >
   <option value="0"></option>
</select>  

Now I caclulate some values here and then send the values to page2.php. Now on page2.php I do some more caclulations and rum a query. If the result of the query is more than 30 records I want the drop down on page1.php reflect that so my users can select the query. so lets say that the result of the query on page2.php was 70, the drop down on page 1 should change to this

<select id="startRecord" >
   <option value="0">0-30</option>
   <option value="30">31-60</option>
   <option value="61">61-70</option>
</select>  

I am thinking I would need innerHtml or .html(). But not sure how to implement this for this case scenario. thanks

+1  A: 

Not sure I completely follow your question but if you are asking how to add items to a dropdown with jquery you can do something like this:

$('#startRecord').append(
    $('<option></option>').val("30").html("31-60")
);

EDIT: Sorry it took so long. We have some people over the house right now. Here you go. Hope this helps:

        $(document).ready(
            function () {
                $("#startRecord option").remove();

                var total = 30;
                var results = 100;
                var lastOption = { Value: -1, End: 0, toString: function () { return this.Value.toString() + "-" + this.End.toString() } };
                var counter = 0;

                while (parseInt(results / total, 10) >= counter) {
                    lastOption.Value = total * counter + (lastOption.Value == -1 ? 0 : 1);
                    lastOption.End = lastOption.Value + total - (lastOption.Value == 0 ? 0 : 1);
                    lastOption.End = (lastOption.End > results) ? results : lastOption.End;
                    $('#startRecord').append(
                        $('<option></option>').val(lastOption.Value.toString()).html(lastOption.toString())
                    );

                    counter++;
                }
            }); 
spinon
i think the OP is asking how to change the contents of the DDL on page 1 using jquery.
marduk
well kinda like that but there is a problem with that.its just adding 31-60 records...what if the query return 100. it will stop at 60. where as in that case we would want 31-60 and 61-90 and 91-100. second thing i noticed is that it appends it which means that when i run that, it adds another same option again and again. thanks
Yeah I was just trying to show how to add a new item to a list. I will give you an example for what you jus said in the comment. Hold please...
spinon
ok I solved all the other problems besides getting it appended multiple times ..is there any way to avoid that. thanks
i will ask question in another post. Thank you for the help!
Not sure I understand what you mean about getting it appended multiple times?
spinon
please read here http://stackoverflow.com/questions/3322883/a-possible-solution-of-jquery-based-dropdown
The code I gave you in the example should work for any scenario you have. Just fill in what the total number of records should be and the total results returned and it will add the correct options. I just used 30 as the example you had. Does that not work for you?
spinon