2012-11-07 78 views
0

我是所有JavaScript和jQuery-Stuff的新手。请原谅我的编码或我肮脏的风格的任何愚蠢的错误。我很高兴您的经验丰富的一方提供所有建设性的反馈意见。创建jQuery元素不在jQuery风格

我尝试动态添加一些jQuery元素,就像一些新的选择菜单一样。在我的例子中,用户应该决定他想要定制的车辆数量,并基于此我想为每辆选定的车辆创建一个新的选择菜单。

我得到它的工作,如果用户选择“2车”jQuery将添加两个新的选择菜单。但我的问题是,这些菜单不再在jQuery-Style中。

我在这里做错了什么?根据汽车的选择数量,是否可以在“for-loop”中添加新的菜单?

这里是我的代码:

<script type="text/javascript"> 

    $('#select-choice-1').live("change", function(event, ui) { 
     var valuee = $(this).val(); 

     if (valuee == '2') ($('<label for="select-choice-2" class="select">Color of car #1</label>' + 
              '<select name="select-choice-2" id="select-choice-2">'+ 
              '<option value="red">red</option>'+ 
              '<option value="blue">blue</option>'+ 
              '</select><br><br>' + 
           '<label for="select-choice-3" class="select">Color of car #2</label>' + 
              '<select name="select-choice-3" id="select-choice-3">'+ 
              '<option value="green">green</option>'+ 
              '<option value="yellow">yellow</option>'+ 
              '</select>').appendTo('#site')); 
     if (valuee == '3') ($('<h1>Test</h1>').appendTo('#site')); 

    }); 
    </script> 

这里是html页面:

<div id="site" data-role="fieldcontain">  

    <label for="select-choice-1" class="select">Number of cars</label> 
    <select name="select-choice-1" id="select-choice-1"> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="3">3</option> 
    </select> 


    </div><!-- "site" --> 
+2

你是什么意思的“jQuery风格”? –

+0

它只是看起来像纯html样式。没有阴影,只是像这样的老派选择领域:http://www.vaannila.com/images/struts1/HtmlSelectTag1Pic1.gif – Stevil

+0

如果通过jQuery风格,你的意思是jQuery-ui样式,那么你需要添加到您的HTML相同的类jQuery的用户界面。 – iMoses

回答

0
 $('#site').delegate("#select-choice-1", "change", function(event, ui) { 
      var valuee = $(this).val(); 
      var constructhtml = ''; 
      if (valuee == '2') { 
       constructhtml +='<label for="select-choice-2" class="select">Color of car #1</label>' + '<select name="select-choice-2" id="select-choice-2">'+ '<option value="red">red</option>'+ '<option value="blue">blue</option>'+ '</select><br><br>' + '<label for="select-choice-3" class="select">Color of car #2</label>' + '<select name="select-choice-3" id="select-choice-3">'+ '<option value="green">green</option>'+ '<option value="yellow">yellow</option>'+ '</select>'; 
      }else if (valuee == '3') { 
       constructhtml +='<h1>Test</h1>'; 
      } 
    $('#site').append(constructhtml); 
    }); 

OP的需要创建一个动态添加选择菜单

将这个档位已到thwe页面最下方

<div id="selectcontainer"></div> 

$(document).ready(function(){ 

var str = ''; 
str+='<select>'; 
    for(i=1;i<=10;i++){ 
    str+='<option value='"+i+"'>'"+i+"'</option>'; 

    } 
str+='</select>'; 
$('#selectcontainer').html(str); 
}); 
+0

最新的区别是什么? – 2619

+0

问题解决了! –

+0

可能是一些CSS设计的问题..我添加了一些调整(删除'活力'和添加格式) – coolguy