jQuery Prev() 问题

发布于 2024-09-24 00:38:28 字数 1551 浏览 2 评论 0原文

抱歉各位,我应该直接发布脚本而不清理它。请检查更新后的脚本,现在应该可以解决问题了。谢谢!

考虑以下 JavaScript :

    var selected = 0;
    var options = 0;

    $('.newListSelected').each(function() {

        selected = $(this).children('.selectedTxt');
        selected = selected.text();

        /* Everything before this line works completely fine */

        options = $(this).prev();

        options.find('option[value=' +selected+ ']').attr('selected', 'selected');

    }).remove();

和 HTML :

<select name="type[]" style="display: none;">
    <optgroup>
        <option value="none">Select</option>
    </optgroup>
    <optgroup label="First Group">
        <option value="1">One</option>
        <option value="2">Two</option>
    </optgroup>
    <optgroup label="Second Group">
        <option value="10">Ten</option>
        <option value="20">Twenty</option>
    </optgroup>
</select>

<div class="newListSelected">
    <div class="selectedTxt">20</div>
    <ul class="newList">
        <!-- Other stuff here -->
    </ul>
</div>

我实际上想做的是将 selected 属性添加到相应的 select option 中,该属性与 < 中的文本具有相同的值代码>.selectedTxt。在上面的代码中,应将 selected="selected" 添加到

然而它没有按预期执行,我还尝试在 prev(); 下面添加 alert(option); 但它没有输出任何有用的东西。

谢谢。

Sorry guys, I should have posted the script directly without cleaning it. Please check the updated script, it should now clear things up. Thanks!

Consider the following JavaScript :

    var selected = 0;
    var options = 0;

    $('.newListSelected').each(function() {

        selected = $(this).children('.selectedTxt');
        selected = selected.text();

        /* Everything before this line works completely fine */

        options = $(this).prev();

        options.find('option[value=' +selected+ ']').attr('selected', 'selected');

    }).remove();

And HTML :

<select name="type[]" style="display: none;">
    <optgroup>
        <option value="none">Select</option>
    </optgroup>
    <optgroup label="First Group">
        <option value="1">One</option>
        <option value="2">Two</option>
    </optgroup>
    <optgroup label="Second Group">
        <option value="10">Ten</option>
        <option value="20">Twenty</option>
    </optgroup>
</select>

<div class="newListSelected">
    <div class="selectedTxt">20</div>
    <ul class="newList">
        <!-- Other stuff here -->
    </ul>
</div>

What I'm trying to do actually is adding the selected attribute to the corresponding select option that has the same value as the text in .selectedTxt. In the code above, it should add selected="selected" to <option value="20">Twenty</option>.

However its not performing as expected, I also tried adding alert(option); below prev(); but it didn't output anything useful.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

酒浓于脸红 2024-10-01 00:38:28

Javascript 工作正常,如下所示: http://jsfiddle.net/4HsXp/

如果您需要能够在 select 元素中选择多个项目,您需要设置 multiplesize 属性,如下所示:

<select multiple="multiple" size="2">
    <option>1</option>
    <option>2</option>
</select>

参见:< a href="http://reference.sitepoint.com/html/select" rel="nofollow noreferrer">http://reference.sitepoint.com/html/select


编辑:

console.log 语句附加到新代码中仍然没有任何问题。 在 jsfiddle 上运行它给了我正确的输出:

jQuery(select)
Original Value: none
New Value: 20

The Javascript works fine, as can be seen here: http://jsfiddle.net/4HsXp/

If you need to be able to select multiple items in a select element, you need to set the multiple and size attribute, like this:

<select multiple="multiple" size="2">
    <option>1</option>
    <option>2</option>
</select>

See: http://reference.sitepoint.com/html/select


Edit:

Attaching console.log statements to the new code still does not any problem. Running it on jsfiddle gave me the correct output:

jQuery(select)
Original Value: none
New Value: 20
孤云独去闲 2024-10-01 00:38:28

我不确定您要做什么,但为什么不能使用正确的选择器选择所有选项元素?如果您只想选择某些选项,则必须添加额外的选择器属性。

$('select option').each(function() {
    $(this).attr('selected', 'selected');
});

I'm not sure what you are trying to do, but why couldn't you just select all option elements with proper selectors? If you want to select only some options, then you have to add extra selector attrbiutes.

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