JQuery 将文本附加到文本字段
抱歉各位,我仍然对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我完全理解您正在寻找的内容,但我将从更一般的意义上解决这个问题:
为了使您的代码在其选择器中寻址的 UL 方面更具限制性:
我们可以使用以下内容:
请注意,我们正在处理从零开始的索引,因此使用“1”来寻址第二个无序列表。
I'm not sure I completely understand what you're looking for, but I'll address this in a more general sense:
To make your code more restrictive with regards to which UL it addresses in its selectors:
We could use the following:
Note that we're dealing with a zero-based index, hence the use of "1" to address the second unordered-list.
我想我明白你在这里问什么,但我可能是错的。假设我这样做,我会添加类或 ID 来表示元素之间的链接。例如:
然后您可以将现有的 JavaScript 重构为:
最终结果是您对控制块、可选择项和字段进行分组,从而允许您根据需要存储选定的值。
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:
Then you could refactor your existing JavaScript into something like:
The end result is that you're grouping the control blocks, selectables, and fields, allowing for you to store the selected values as desired.