jQuery选择器:我可以将.class用于选择列表吗? [英] jQuery selectors: Can I use .class for Select Lists?
问题描述
我将有一个选择字段列表,收集相同类型的数据.例如,我将有一些选择列表,标记为相同的类.
I am going to have a list of select fields, collecting the same type of data. For example, I'll have a few select lists, tagged to the same class.
<select name="NBSCourse1" class="NBSCourse"></select>
<select name="NBSCourse2" class="NBSCourse"></select>
<select name="NBSCourse3" class="NBSCourse"></select>
// and so on...
我已经编写了一种方法来检测选择列表中的更改,所有这些更改都是按类引用的.
I've written a method to detect change in the select lists, all referring to them by class.
$("select.NBSCourse").change(function() {
//Other codes here
})
似乎第一个选择列表(name ="NBSCourse1")有效,但其他选择列表不起作用.如果有多个类似的列表,我不应该对选择列表使用类吗?
It seems that the first select list (name="NBSCourse1") works, but the other select lists do not work. Am I not supposed to use classes for select lists, if there are multiple similar ones?
有关我要执行的操作的详细信息...
实际上,选择字段的第一行已由我编写的jQuery函数初始化:addrow(n).在$(document).ready上调用该方法.
Actually, the first row of Select Fields has been initialized by a jQuery function I've wrote: addrow(n). The method is called during, on $(document).ready.
我还有一个按钮添加更多",它将调用addrow(n)函数.我已经声明了一个称为"counterNBSCourse"的计数器,该计数器已传递给该函数. addrow(counterNBSCourse).
I also have a button "Add More", which will call the addrow(n) function. I've declared a counter that's called 'counterNBSCourse', which I passed into the function. addrow(counterNBSCourse).
function addRow(n){
var tablerow
tablerow = "<tr>";
tablerow += "<td><select name='NBSCourse" + n + "' class='NBSCourse'></select></td>";
//Other select fields
tablerow += "</tr>"
$("table.courses").append(tablerow);
//Method to populate select list
$.ajax({url:"dropdown.asp?type=courseidfill", success:function(result) {
$("select[name=NBSCourse" + n + "]").html(result);
}})
counterNBSCourse++
}
如您所见,当我添加具有类"NBSCourse"的选择字段的另一行时,我的名字是根据"counterNBSCourse"的当前值得出的. "NBSCourse1","NBSCourse2"等.
As you can see, when I add another row of select fields that has a class "NBSCourse", my is name according to the current value of "counterNBSCourse". "NBSCourse1","NBSCourse2".. etc.
此刻,仅响应该字段.
$("select.NBSCourse").change(function(){
Further notes of the $("select.NBSCourse").change(function() {
$("select.NBSCourse").change(function () {
//Getting select field's name
var name
name = $(this).attr('name');
//Getting the number tagged behind the select name file
var num
num = name.replace("NBSCourse","");
//Getting selected value
var nbsCourseid;
nbsCourseid = $(this).val();
//Function to populate another select field,
$.ajax({url:"dropdown.asp?value="+ nbsCourseid + "&type=courseid",success:function(result) {
$("select[name=IndexNo" + num + "]").html(result);
}})
})
推荐答案
可以使用类选择器,您会错过代码$("select.NBSCourse)
的"
.
It's ok to use class selector, you miss a "
of your code $("select.NBSCourse)
.
这篇关于jQuery选择器:我可以将.class用于选择列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!