我是所有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" -->
你是什么意思的“jQuery风格”? –
它只是看起来像纯html样式。没有阴影,只是像这样的老派选择领域:http://www.vaannila.com/images/struts1/HtmlSelectTag1Pic1.gif – Stevil
如果通过jQuery风格,你的意思是jQuery-ui样式,那么你需要添加到您的HTML相同的类jQuery的用户界面。 – iMoses