jQuery find() 从字符串创建的对象
我有一个字符串,其值例如 。
当我想弹出该选择时,我想到做这样的事情来找到选择,然后弹出它:
$("<select name="id"></select>").find('select').html('<option value="1">Test</option>;
但是,似乎 $("
我做错了什么?
编辑:好的,发布整个代码:
我有一个 元素,其中包含原型表单:
<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();">
I have a string with a value such as <select name="id"></select>
.
As I'd like to popupate that select, I thought of doing something like this to find the select, and popupate it afterwards:
$("<select name="id"></select>").find('select').html('<option value="1">Test</option>;
However, it seems that $("<select name="id"></select>")
doesn't match anything.
What am I doing wrong?
Edit: Okay, posting the whole code:
I have a <script type="text/html">
element with a prototype form in it:
<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();">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Find 不会在您的
字符串中找到任何内容,因为它正在 select 标记内查找“select”元素。有关详细信息,请参阅此答案 -
使用 jQuery 搜索 HTML 字符串< /a>
你可以尝试这样的事情 -
工作演示 - http://jsfiddle.net/ipr101/U5LPw/
Find won't find anything in your
<select name="id"></select>
string as it's looking for a 'select' element inside the select tags. See this answer for further info -Using jQuery to search a string of HTML
You could try something like this -
Working demo - http://jsfiddle.net/ipr101/U5LPw/
你需要将它附加到
这里的东西是小提琴 http://jsfiddle.net/hRFYF/5/
you need to append it to something
here is the fiddle http://jsfiddle.net/hRFYF/5/
您正在创建一个新的
select
元素,因此您获得的 jQuery 对象仅包含该元素。使用find
搜索该元素内部的匹配项,因此自然不会有匹配项。您根本不需要使用
find
,因为 jQuery 对象仅包含该单个元素:(此外,您在由引号分隔的字符串内使用引号,没有匹配的结束撇号包含选项的字符串,并且
html
方法调用没有结束括号。)You are creating a new
select
element, so the jQuery object that you get only contains that element. Usingfind
searches for matches inside that element, so naturally there will be no match.You don't need to use
find
at all, as the jQuery object only contains that single element:(Also, you were using quotation marks inside a string delimited by quotation marks, there was no matching end apostrophe for the string containing the option, and there was no ending parenthesis for the
html
method call.)您正在创建一个新元素,而不是选择现有元素。如果您不将该元素
附加
到 DOM,您将看不到结果。尝试:如果您确实打算选择现有元素,请更改选择器:
You're creating a new element, not selecting an existing element. If you don't then
append
that element to the DOM, you won't see the result. Try:If you actually intend to select the existing element, change the selector: