jQuery:将参数传递给函数
我想将参数传递到我的函数中。该参数将是一个代表 CSS 类名的变量。根据选择的复选框,我将设置变量的值(例如,如果选择了 CheckBox1,则变量的值将是类名“.list1”)。
下拉列表有大量选项,我想根据“类”选择选项的子集,并使用这些选项填充另一个列表(fl_listEmpty)。
据我所知,这是我的代码:
function FillDropDownList(parameter) {
$('.fl_list1 [**put class name parameter here**]).each(function (index) {
$(".fl_listEmpty").append($('<option> </option>').val(index).html($(this).text()));
})
}
I want to pass a parameter into my function. The parameter will be a variable that representsa CSS class name. Based upon which checkbox was selected I will set the value of the variable (eg, if CheckBox1 was selected the value of the variable would be the class name '.list1').
The dropdownlist has a large number of options and I want to select a subset of the options based on their 'class' and use those options to populate another list (fl_listEmpty).
Here is my code as far as I've gotten:
function FillDropDownList(parameter) {
$('.fl_list1 [**put class name parameter here**]).each(function (index) {
$(".fl_listEmpty").append($('<option> </option>').val(index).html($(this).text()));
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将选择器视为一个字符串即可(事实确实如此)。
Just compose the selector as if it were a string (which it is).
假设你在课间有意留出空间,那么找到一个合适的空间。如果该值可能未定义,您可以设置一个条件:
Assuming your space is intentional between classes, a find would be appropriate. If it is possible that the value may be undefined, you could just make a condition: