获取动态创建的文本框id jquery

时间:2013-11-16 07:45:54

标签: javascript jquery

我使用jquery使用for循环动态创建html代码,我需要获取动态创建的img的 ID ,我怎样才能获得 ID ?< / p>

这是jquery代码

var subscriptionDiv = $(document.createElement('div')).attr("id", 'subscription' + counter);
             subscriptionDiv.html('<div class="form-group">'+
                                    '<div class="fieldBlock">'+
                                        '<div class="col-lg-10">'+
                                            '<label for="meal" class="col-lg-2 control-label" style="width: 138px;">Date: '+dateArray[j]+'</label>'+
                                        '</div>'+
                                    '</div>'+
                                   '</div>'+
                                   '<div class="fieldBlock">'+
                                      '<label for="meal" class="col-lg-2 control-label" style="width: 138px;">Meal Option:</label>'+
                                      '<div class="col-lg-10">'+
                                            '<select class="dropdown rightside" id="meal1-'+mealArray[i]+'-'+counter+'" name="meal-'+counter+'" style="width: 220px;">'+
                                                 '<option value=""></option>'+
                                                 '<option value="Breakfast" id="b">Breakfast</option>'+
                                                 '<option value="Lunch" id="l">Lunch</option>'+
                                                 '<option value="Dinner" id="d">Dinner</option>'+
                                            '</select>'+
                                      '</div>'+
                                   '</div>'+
                                   '<div class="fieldBlock">'+
                                             '<label for="breakfast" class="col-lg-2 control-label" style="width: 120px;">Breakfast</label>'+
                                       '<div class="col-lg-10">'+
                                             '<select class="dropdown fieldBlockta" name="hrs-'+counter+'" style="width: 55px;" id="hrs-'+counter+'">'+
                                                    '<option value="hrs">hrs</option>'+
                                                    '<option value="0">0</option>'+
                                                    '<option value="1">1</option>'+
                                                    '<option value="2">2</option>'+
                                                    '<option value="3">3</option>'+
                                                    '<option value="4">4</option>'+
                                                    '<option value="5">5</option>'+
                                                    '<option value="6">6</option>'+
                                                    '<option value="7">7</option>'+
                                                    '<option value="8">8</option>'+
                                                    '<option value="9">9</option>'+
                                                    '<option value="10">10</option>'+
                                                    '<option value="11">11</option>'+
                                                    '<option value="12">12</option>'+
                                             '</select>'+
                                             '<select class="dropdown fieldBlockta" name="mins-'+counter+'" style="width: 65px; margin-left: 20px;" id="mins-'+counter+'">'+
                                                    '<option value="mins">mins</option>'+
                                                    '<option value="0">0</option>'+
                                                    '<option value="1">1</option>'+
                                                    '<option value="2">2</option>'+
                                                    '<option value="3">3</option>'+
                                                    '<option value="4">4</option>'+
                                                    '<option value="5">5</option>'+
                                                    '<option value="6">6</option>'+
                                                    '<option value="7">7</option>'+
                                                    '<option value="8">8</option>'+
                                                    '<option value="9">9</option>'+
                                                    '<option value="10">10</option>'+
                                                    '<option value="11">11</option>'+
                                                    '<option value="12">12</option>'+
                                                    '<option value="13">13</option>'+
                                                    '<option value="14">14</option>'+
                                                    '<option value="15">15</option>'+
                                                    '<option value="16">16</option>'+
                                                    '<option value="17">17</option>'+
                                                    '<option value="18">18</option>'+
                                                    '<option value="19">19</option>'+
                                                    '<option value="20">20</option>'+
                                                    '<option value="21">21</option>'+
                                                    '<option value="22">22</option>'+
                                                    '<option value="23">23</option>'+
                                                    '<option value="24">24</option>'+
                                                    '<option value="25">25</option>'+
                                                    '<option value="26">26</option>'+
                                                    '<option value="27">27</option>'+
                                                    '<option value="28">28</option>'+
                                                    '<option value="29">29</option>'+
                                                    '<option value="30">30</option>'+
                                                    '<option value="31">31</option>'+
                                                    '<option value="32">32</option>'+
                                                    '<option value="33">33</option>'+
                                                    '<option value="34">34</option>'+
                                                    '<option value="35">35</option>'+
                                                    '<option value="36">36</option>'+
                                                    '<option value="37">37</option>'+
                                                    '<option value="38">38</option>'+
                                                    '<option value="39">39</option>'+
                                                    '<option value="40">40</option>'+
                                                    '<option value="41">41</option>'+
                                                    '<option value="42">42</option>'+
                                                    '<option value="43">43</option>'+
                                                    '<option value="44">44</option>'+
                                                    '<option value="45">45</option>'+
                                                    '<option value="46">46</option>'+
                                                    '<option value="47">47</option>'+
                                                    '<option value="48">48</option>'+
                                                    '<option value="49">49</option>'+
                                                    '<option value="50">50</option>'+
                                                    '<option value="51">51</option>'+
                                                    '<option value="52">52</option>'+
                                                    '<option value="53">53</option>'+
                                                    '<option value="54">54</option>'+
                                                    '<option value="55">55</option>'+
                                                    '<option value="56">56</option>'+
                                                    '<option value="57">57</option>'+
                                                    '<option value="58">58</option>'+
                                                    '<option value="59">59</option>'+
                                             '</select>'+
                                       '</div>'+
                                   '</div>'+
                                   '<div class="fieldBlock">'+
                                       '<label for="location" class="col-lg-2 control-label" style="width: 138px;">Location:</label>'+
                                       '<div class="col-lg-10">'+
                                           '<select class="dropdown rightside" style="width: 220px;" id="location-'+counter+'">'+
                                               '<option value=""></option>'+
                                               '<option value="Office">Office</option>'+
                                               '<option value="Residence">Residence</option>'+
                                               '<option value="GirlFriend">Girl Friend</option>'+
                                           '</select>'+
                                       '</div>'+
                                   '</div>'+
                                   '<div class="fieldBlock">'+
                                       '<label for="address" class="col-lg-2 control-label" style="width: 245px;">Address plus instruction for delivery</label>'+
                                       '<div class="col-lg-10">'+
                                           '<textarea class="textarea" id="address-'+counter+'" required="required" rows="5" cols="50" placeholder="Address plus instruction for delivery"></textarea>'+
                                       '</div>'+
                                   '</div>'+
                                   '<div class="fieldBlock">'+
                                   '<div class="form-group">'+
                                       '<div class="col-lg-10">'+
                                           '<img id="add-'+counter+'" style="height:30px; margin-top:30px;" src="/images/add.png"/>'+
                                       '</div>'+
                                   '</div>'+
                                   '<div class="form-group">'+
                                       '<div class="col-lg-10">'+
                                           '<img id="minus-'+counter+'" style="height:30px; margin-top:30px;" src="/images/minus.png"/>'+
                                       '</div>'+
                                   '</div>'+
                                   '</div>'+
                                   '<div class="fieldBlock">'+
                                       '<div class="col-lg-10">'+
                                           '<button type="button" id="subscribeNow-'+counter+'" name="subscribeNow-'+counter+'" class="btn btn-primary subscribeNow" style="">Save & choose eatery</button>'+
                                       '</div>'+
                                   '</div>');
                                    $('select option[value="meal1-'+mealArray[i]+'-'+counter+'"]').attr("selected",true);
             subscriptionDiv.appendTo("#subscribe");
             counter=counter+1;
             }
             }


        });

我需要获取动态创建的img源代码

<img id="minus-'+counter+'" style="height:30px; margin-top:30px;" src="/images/minus.png"/>

2 个答案:

答案 0 :(得分:0)

试试这个

$('img[id^="minus-"]').each(function (i,el) {
   var src = $(this).attr('src');
   // or what ever you want to do with this element/s..
   var counter = $(this).attr('id').match(/\d+/g);
   if ( counter[0] == 10 ) {
       // do something for the img with id `minus-10`
   }
});

答案 1 :(得分:0)

尝试这样的事情

     $('.form-group img').each(function(){
        alert(this.id);
     })