JQuery 将文本附加到文本字段

发布于 2024-08-18 16:35:11 字数 1084 浏览 5 评论 0原文

抱歉各位,我仍然对 JQuery 很感兴趣。对创建像这样的可选 ul li 列表有很大帮助

$(document).ready(function(){

    $('.selectoption li').not(':first').hide();

    $('.prev, .next').click(function() {
        // Determine the direction
        var dir = $(this).hasClass('prev') ? 'prev' : 'next';
        // Get the li that is currently visible
        var current = $('.selectoption li:visible');

        // Get the element that should be shown next according to direction
        var next = dir == 'prev' ? current.prev('li') : current.next('li');

        // If there's no more in that direction, select first/last
        if(next.size() == 0) {
            next = dir == 'prev' ? $('.selectoption li:first') : $('.selectoption li:first');
        }

        // Hide them all..
        $('.selectoption li').hide();
        // And show the new one
        next.show();

        return false;
    });

});  

但是我怎样才能将所选 li 的值附加到文本字段中,以便可以在表单中使用它 - 在这种情况下不能使用 Ajax 等,因为 li 列表在外部形式。

另外,如果我在一个页面上有 3 或 4 个这样的 ul li,我该如何包装上面的代码,使下一个/上一个按钮仅与它们应用的 ul li 一起工作。

提前致谢

Sorry folks, still being thick on JQuery. Had great help creating a selectable ul li list like this

$(document).ready(function(){

    $('.selectoption li').not(':first').hide();

    $('.prev, .next').click(function() {
        // Determine the direction
        var dir = $(this).hasClass('prev') ? 'prev' : 'next';
        // Get the li that is currently visible
        var current = $('.selectoption li:visible');

        // Get the element that should be shown next according to direction
        var next = dir == 'prev' ? current.prev('li') : current.next('li');

        // If there's no more in that direction, select first/last
        if(next.size() == 0) {
            next = dir == 'prev' ? $('.selectoption li:first') : $('.selectoption li:first');
        }

        // Hide them all..
        $('.selectoption li').hide();
        // And show the new one
        next.show();

        return false;
    });

});  

But how can I then append the value of the selected li into a text field so it can be used within a form - cannot use Ajax etc. on this occassion as the li list is outside the form.

Also, if I have say 3 or 4 of these ul li's on a page how do I wrap the above code in a way that the next / prev button only wotk with the ul li that they apply to.

Thanks in advance

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

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

发布评论

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

评论(2

翻了热茶 2024-08-25 16:35:11

我不确定我完全理解您正在寻找的内容,但我将从更一般的意义上解决这个问题:

$("li").click(function(){
  // Place LI text as value of <input type="text" name='fname' />
  $(":input[name='fname']").val($(this).text());
});

为了使您的代码在其选择器中寻址的 UL 方面更具限制性:

<ul>
  <li class="selectOption">Ignore Me</li>
</ul>
<ul>
  <li class="selectOption">Include Me</li>
</ul>

我们可以使用以下内容:

$("ul:eq(1) .selectOption").click(function(){
  // only the LI in the second UL will trigger this
});

请注意,我们正在处理从零开始的索引,因此使用“1”来寻址第二个无序列表。

I'm not sure I completely understand what you're looking for, but I'll address this in a more general sense:

$("li").click(function(){
  // Place LI text as value of <input type="text" name='fname' />
  $(":input[name='fname']").val($(this).text());
});

To make your code more restrictive with regards to which UL it addresses in its selectors:

<ul>
  <li class="selectOption">Ignore Me</li>
</ul>
<ul>
  <li class="selectOption">Include Me</li>
</ul>

We could use the following:

$("ul:eq(1) .selectOption").click(function(){
  // only the LI in the second UL will trigger this
});

Note that we're dealing with a zero-based index, hence the use of "1" to address the second unordered-list.

风流物 2024-08-25 16:35:11

我想我明白你在这里问什么,但我可能是错的。假设我这样做,我会添加类或 ID 来表示元素之间的链接。例如:

<input type="hidden" name="degree" id="degree" />
<div id="degree_selectable_control" class="selectable_control">
    <a href="#" class="prev">Prev</a> |
    <a href="#" class="next">Next</a>
</div>
<ul id="degreee_selectable" class="selectable">
    <li>Associates</li>
    <li>Bachelor's</li>
    <li>Graduate</li>
    <li>Other</li>
</ul>

<input type="hidden" name="salary" id="salary" />
<div id="salary_selectable_control" class="selectable_control">
    <a href="#" class="prev">Prev</a> |
    <a href="#" class="next">Next</a>
</div>
<ul id="salary_selectable" class="selectable">
    <li>> $20,000</li>
    <li>$20,000 - $35,000</li>
    <li>$35,000 - $50,000</li>
    <li>< $50,000</li>
</ul>

然后您可以将现有的 JavaScript 重构为:

$(document).ready(function(){
    // init the selectables
    $('.selectable').each(function(){
        $(this).find('li').not(':first').hide();
    });
    // assign the handlers for each control
    $('.selectable_control').each(function(){
        var field_id = this.id.replace(/_selectable_control$/, ''),
            $selectable = $('#' + field_id + '_selectable'),
            $field = $('#' + field_id);

        $(this).find('.prev, .next').click(function(){
            // Determine the direction
            var dir = $(this).hasClass('prev') ? 'prev' : 'next';
            // Get the li that is currently visible
            var current = $selectable.find('li:visible');

            // Get the element that should be shown next according to direction
            var next = dir == 'prev' ? current.prev('li') : current.next('li');

            // If there's no more in that direction, select first/last
            if(next.length === 0) {
                next = dir == 'prev' ? $selectable.find('li:last') : $selectable.find('li:first');
            }

            // Hide them all..
            $selectable.find('li').hide();
            // And show the new one
            next.show();
            // And update the corresponding field
            $field.val( next.text() );

            return false;
        });
    });
});

最终结果是您对控制块、可选择项和字段进行分组,从而允许您根据需要存储选定的值。

I think I understand what you're asking here, but I could be wrong. Assuming I do, I would add classes or IDs to represent a linkage between the elements. For instance:

<input type="hidden" name="degree" id="degree" />
<div id="degree_selectable_control" class="selectable_control">
    <a href="#" class="prev">Prev</a> |
    <a href="#" class="next">Next</a>
</div>
<ul id="degreee_selectable" class="selectable">
    <li>Associates</li>
    <li>Bachelor's</li>
    <li>Graduate</li>
    <li>Other</li>
</ul>

<input type="hidden" name="salary" id="salary" />
<div id="salary_selectable_control" class="selectable_control">
    <a href="#" class="prev">Prev</a> |
    <a href="#" class="next">Next</a>
</div>
<ul id="salary_selectable" class="selectable">
    <li>> $20,000</li>
    <li>$20,000 - $35,000</li>
    <li>$35,000 - $50,000</li>
    <li>< $50,000</li>
</ul>

Then you could refactor your existing JavaScript into something like:

$(document).ready(function(){
    // init the selectables
    $('.selectable').each(function(){
        $(this).find('li').not(':first').hide();
    });
    // assign the handlers for each control
    $('.selectable_control').each(function(){
        var field_id = this.id.replace(/_selectable_control$/, ''),
            $selectable = $('#' + field_id + '_selectable'),
            $field = $('#' + field_id);

        $(this).find('.prev, .next').click(function(){
            // Determine the direction
            var dir = $(this).hasClass('prev') ? 'prev' : 'next';
            // Get the li that is currently visible
            var current = $selectable.find('li:visible');

            // Get the element that should be shown next according to direction
            var next = dir == 'prev' ? current.prev('li') : current.next('li');

            // If there's no more in that direction, select first/last
            if(next.length === 0) {
                next = dir == 'prev' ? $selectable.find('li:last') : $selectable.find('li:first');
            }

            // Hide them all..
            $selectable.find('li').hide();
            // And show the new one
            next.show();
            // And update the corresponding field
            $field.val( next.text() );

            return false;
        });
    });
});

The end result is that you're grouping the control blocks, selectables, and fields, allowing for you to store the selected values as desired.

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