我有一个字符串,其值为<select name="id"></select>
。从一个字符串创建的对象上的jQuery find()
正如我想popupate是选择,我认为做这样的事情,找到选择的,事后popupate它:
$("<select name="id"></select>").find('select').html('<option value="1">Test</option>;
然而,似乎$("<select name="id"></select>")
不匹配任何东西。
我在做什么错?
编辑:好吧,张贴整个代码:
我有一个原型形式的<script type="text/html">
元素在里面:
<ul id="advanced_search_fields"></ul>
<script type="text/html" id="prototype_fields">
<select name="search[__i__][field]" class="field_selector">
<option></option>
<option data-type="choice" value="10">Bank</option>
</select>
</script>
<script type="text/javascript">
var field_config = {"10":{"choices":["Choice 1","Choice 2","Choice 3","Other"]}};
</script>
<script type="text/html" id="prototype_choice">
<select name="search[__i__][value]"></select>
</script>
<script type="text/javascript">
var srch_list = $('#advanced_search_fields');
var add = function(){
srch_list.append('<li>' + $('#prototype_fields').html() + '</li>');
};
var change_field = function(){
$(this).parent().find('span.field').remove();
$(this).parent().data('id', $(this).val());
change_field_to_type($(this).val(), $(this).find('option:selected').data('type'), $(this).parent());
};
var change_field_to_type = function(id, type, li){
var prototype = $('#prototype_' + type).html();
var formHtml = "";
switch(type)
{
case "choice":
var select = $(prototype).filter('select');
$(field_config[id]['choices']).each(function(i, val){
select.append(
$('<option></option>')
.attr('value', val)
.text(val)
);
});
formHtml = prototype;
break;
}
li.append('<span class="field">' + formHtml + '</span>');
};
$('.field_selector').live('change', change_field);
add();
</script>
<input type="button" value="+" onclick="add();">
Regading您的编辑;在实际使用的代码中找不到原始问题的代码。那么,代码的哪部分是你有问题? – Guffa