priyankirk
priyankirk

Reputation: 45

How to handle event of Dynamic select menu in Jquery mobile?

I'm making a dynamic select menu using Jquery mobile. First problem that i am facing is that Placeholders are not appearing for both ie statically created option "--select day--" and "--select time--" which i have statically created in html file. It is just loading the dynamically created values. Second problem and the Important one is that I want to load "PMtime" if user selects the "Today-Drop", and "AMtime" if user selects the "Tomorrow-Pick up" option. I created an event handler for it but it seems not working. Please help.... HTML code:

<body onload="init();">
<form method="get" name="datarange">
<div data-role="fieldcontain">

    <select id="select-choice-1" name="select-choice-1"  class="target" >
        <option data-placeholder="true">--Select Day--</option>

  </select>
    </div>
<div data-role="fieldcontain">
<select id="select-choice-2" name="select-choice-2" >
    <option data-placeholder="true">--Select Time--</option>

</select>
    <script>
        $('.target').change(function(){
                            var str="";
                            $("select option:selected".each(function(){
                                                            str+=$(this).text() + "";
                                                            });
                              if(str.text=="Today-Drop")
                              {
                              check=0;
                              }
                              else
                              {
                              check=1;
                              }
                            Load_selected_time(check);

                            })
                            .trigger('change');

   </script>
    </div>

And in my JS file

var check=0;
var day=[
     {name : "Today-Drop",id: "drop"},
     {name : "Tomorrow-Pick up",id: "pickup"}];
var PMtime=[
        {name : "3:00 PM", id: "3"},
        {name : "4:00 PM", id: "4"},
        {name : "7:30 PM", id: "730"},
        {name : "9:00 PM", id: "9"},
        {name : "10:15 PM",id: "1015"},
        {name : "11:00 PM",id: "11"}];
var AMtime=[
        {name : "12:00 AM", id: "12"},
        {name : "1:00 AM" ,id: "1"},
        {name : "2:00 AM" ,id: "2"},
        {name : "3:00 AM" ,id: "3am"},
        {name : "4:00 AM" ,id: "4"},
        {name : "5:00 AM" ,id: "5"},
        {name : "6:00 AM" ,id: "6"},
        {name : "7:00 AM" ,id: "7"},
        {name : "8:00 AM" ,id: "4"},
        {name : "9:00 AM" ,id: "5"}];

function init(){
                 Load_selected();}
function Load_selected() {

var arraytoload = day;
var  optionlist = '';
var i = 0;

$.each(arraytoload, function (index,item) {
       if (i == 0) {
       optionlist += '<option selected="' + item.id + '">' + item.name + '</option>';
       }
       else {
       optionlist += '<option value="' + item.id + '">' + item.name + '</option>';
       }
       i++;
       })
optionlist += '';
$("#select-choice-1").html(optionlist).selectmenu('refresh', true);
}

function Load_selected_time(check){
if(check==0){
    var arraytoload = PMtime;}
else{
    var arraytoload = AMtime;
}
var  optionlist = '';
var i = 0;

$.each(arraytoload, function (index,item) {
       if (i == 0) {
       optionlist += '<option selected="' + item.id + '">' + item.name + '</option>';
       }
       else {
       optionlist += '<option value="' + item.id + '">' + item.name + '</option>';
       }
       i++;
       })
optionlist += '';
$("#select-choice-2").html(optionlist).selectmenu('refresh', true);
}

Upvotes: 0

Views: 691

Answers (2)

Hans Hohenfeld
Hans Hohenfeld

Reputation: 1739

Your first problem (placeholders not being shown) occurs, because you're overwriting the content of both .

Try to initialize your 'optionslist' string in both functions with

var optionslist = $('yourselect').html();

before adding the items to that string in the 'each' loop.

Upvotes: 0

root
root

Reputation: 1583

use .on()

 $('.target').on('change',function(){  

  // your code here.

});

Upvotes: 1

Related Questions