jQuery find() 从字符串创建的对象

发布于 2024-12-02 13:05:00 字数 2388 浏览 0 评论 0原文

我有一个字符串,其值例如

当我想弹出该选择时,我想到做这样的事情来找到选择,然后弹出它:

$("<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落墨 2024-12-09 13:05:01

Find 不会在您的 字符串中找到任何内容,因为它正在 select 标记内查找“select”元素。有关详细信息,请参阅此答案 -

使用 jQuery 搜索 HTML 字符串< /a>

你可以尝试这样的事情 -

var html= '<select name="id"></select>';
var $select = $('<div></div>').append(html);
$select.find('select').append('<option value="1">Test</option>');
alert($select.html());

工作演示 - 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 -

var html= '<select name="id"></select>';
var $select = $('<div></div>').append(html);
$select.find('select').append('<option value="1">Test</option>');
alert($select.html());

Working demo - http://jsfiddle.net/ipr101/U5LPw/

物价感观 2024-12-09 13:05:01

你需要将它附加到

$("<select name='id'></select>").append('<option value="1">Test</option>').appendTo("div");

这里的东西是小提琴 http://jsfiddle.net/hRFYF/5/

you need to append it to something

$("<select name='id'></select>").append('<option value="1">Test</option>').appendTo("div");

here is the fiddle http://jsfiddle.net/hRFYF/5/

ペ泪落弦音 2024-12-09 13:05:01

您正在创建一个新的 select 元素,因此您获得的 jQuery 对象仅包含该元素。使用 find 搜索该元素内部的匹配项,因此自然不会有匹配项。

您根本不需要使用 find ,因为 jQuery 对象仅包含该单个元素:(

var sel = $('<select name="id"></select>').html('<option value="1">Test</option>');

此外,您在由引号分隔的字符串内使用引号,没有匹配的结束撇号包含选项的字符串,并且 html 方法调用没有结束括号。)

You are creating a new select element, so the jQuery object that you get only contains that element. Using find 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:

var sel = $('<select name="id"></select>').html('<option value="1">Test</option>');

(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.)

柳若烟 2024-12-09 13:05:01

您正在创建一个元素,而不是选择现有元素。如果您不将该元素附加到 DOM,您将看不到结果。尝试:

$("<select name=\"id\"></select>").appendTo($("body"));

如果您确实打算选择现有元素,请更改选择器:

$("select[name='id']")...

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:

$("<select name=\"id\"></select>").appendTo($("body"));

If you actually intend to select the existing element, change the selector:

$("select[name='id']")...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文