我如何在函数中使jquery id选择等于这个
How do I make a jquery id selection equal to this inside a function?
为什么我得到一个错误,当我试图使$("#select-province")
that.value
?
function updateCities(that){
console.log(that.value);
}
$(document).ready(function(){
updateCities($("#select-province"));
});
您需要将jquery对象转换为javascript object.use:
function updateCities(that){
console.log(that.value);
}
$(document).ready(function(){
updateCities($("#select-province")[0]);
});
选择1,但不能两者都选:
-
传递一个DOM对象,而不是jQuery对象
function updateCities(that){ console.log(that.value); } $(document).ready(function(){ updateCities(document.getElementById('select-province')); });
-
因为你正在传递一个jQuery对象,使用库的函数来正确检索值:
function updateCities($that){ console.log($that.val()); // or $that.prop('value') } $(document).ready(function(){ updateCities($("#select-province")); });
注意,我将
that
的名称改为$that
,以反映它是一个jQuery对象
在使用jQuery时,要尽可能地使用jQuery
function updateCities(that){
console.log($(that).val());
}
$(document).ready(function(){
updateCities($("#select-province"));
});
使用jquery $(that).val()效果良好
function updateCities(that){
console.log($(that).val());
}
$(document).ready(function(){
updateCities($("#select-province"));
});
参见提琴链接http://jsfiddle.net/5AX6p/1/
相关文章:
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 获取打开jquery对话框的button的id
- jQuery mobile not going through ID
- 如何将li类更改为li id's在Jquery中
- 如何使用jQuery迭代id名称的变体(如idname1、idname2)
- 获取不带Id JQuery的复选框值
- 从ID Jquery类获取返回值时出现问题
- attr() 不会更改 id - jquery
- 通过 ID jquery 限制文本区域字符
- 获取选定的行id jquery数据表行选择
- 如何在asp.net mvc中向Action方法发布简单ID(jquery-ajax)
- 添加文本到Javascript Span id jquery
- 动态获取控件ID - JQuery
- 验证选择字段取决于id jquery
- Increment id jquery
- 使用id jquery中的空格
- 根据HTML表的ID jQuery/javaScript对数据进行排序
- 以引号结束id - jquery
- 当draggable被拖放到可拖放区域时添加ID - jQuery Draggables
- 表格ID &jQuery中的名称