jquery和设置值范围

发布于 2024-08-13 09:37:07 字数 628 浏览 4 评论 0原文

我试图想出一种使用 ajax 和 jquery 来显示选择框的一系列值的方法。 第一个下拉菜单包含需要与第二个下拉框对话的选项,这是一个数字范围,我知道如何将下拉菜单的值设置为 1 个值,但不知道如何设置整个范围。默认情况下,下拉列表是由 php range 函数生成的。

任何帮助将不胜感激。

更新 这不是最优雅的,但这是我所拥有的

$('.change').change(function(){
var options='';
var max = $('.change option:selected').attr('title');
var min = $('.change option:selected').attr('id');


for(i=min; i <= max; i++){
    options+='<option value="'+i+'">'+i+'</option>';
}
$('select[name=ticket_qty]').find("option").remove();

$('select[name=ticket_qty]').append(options);


});

我在提取所选选项的“ID”和“TITLE”时遇到问题

I am trying to come up with a way using ajax and jquery to display a range of values for a select box.
The first drop down has options that need to talk to a 2nd drop box and that is a range of numbers, I know how to set the value of a drop down to 1 value, but not how set the whole range. By default the dropdowns are being generated by php range function.

Any help would be appreciated.

Update
This is not the most elegant, but here is what i have

$('.change').change(function(){
var options='';
var max = $('.change option:selected').attr('title');
var min = $('.change option:selected').attr('id');


for(i=min; i <= max; i++){
    options+='<option value="'+i+'">'+i+'</option>';
}
$('select[name=ticket_qty]').find("option").remove();

$('select[name=ticket_qty]').append(options);


});

I am having trouble pulling the "ID" and "TITLE" for the selected option

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

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

发布评论

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

评论(3

污味仙女 2024-08-20 09:37:07

如果列表很大,您可能需要使用类似以下内容的内容:

var options='';
for(i=1; i <= 1234; i++){
    options+='<option value="'+i+'">'+i+'</option>';
}
$(yourSelect).append(options);

if the list is large you might want to use something like:

var options='';
for(i=1; i <= 1234; i++){
    options+='<option value="'+i+'">'+i+'</option>';
}
$(yourSelect).append(options);
倾听心声的旋律 2024-08-20 09:37:07

如果这些是数值,您可能需要考虑使用 jQuery UI Slider。否则,您只需要通过附加到选择标签来删除/添加选择框中的选项。

在第一个文本框发生更改事件时,调用一个函数来修改选择框 2 中的选项,删除不再可行的值。

function addValue(value, target) {
  $('<option value="' + value + '">' + value + '</option>').appendTo( target ); 
}

function clearValues (target) {
  $(target).find("option").remove();
}

然后,您可以按如下方式调用该函数:

addValue(12,$("#select2 input") );

注意:我尚未测试此代码,它只是一种方法的一般概念。

对此效果的某些处理可能会很有用。

If these are numerical values, you might want to look into using the jQuery UI Slider. Otherwise, you'll just need to remove/add options in the select box by appending to the select tag.

On the change event of your first text box, call a function that will modify the options in select box 2, removing values that are no longer viable.

function addValue(value, target) {
  $('<option value="' + value + '">' + value + '</option>').appendTo( target ); 
}

function clearValues (target) {
  $(target).find("option").remove();
}

You would then call the function as follows:

addValue(12,$("#select2 input") );

Note: I haven't tested this code, it's just a general idea of an approach.

Someting to that effect might be useful.

桃扇骨 2024-08-20 09:37:07

您将需要迭代选择框 (element.options) 中的项目,然后在给定属性上设置 .selected 属性。

这样您就可以设置多个选择。

IE。 element.options[i].selected = true

You will need to iterate over the items in the select box (element.options) and then set the .selected property on a given property.

In that way you can set multiple selections.

ie. element.options[i].selected = true

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