$(this).find('select option:selected').each(function (i, val) {
var selectedValue = $(val).val(); //this returns the text from the option instead of the value
});
這是ie7中的一個已知問題嗎?我沒有找到任何有關它的信息?可能的解決方法?由於val()on <option>在ie7中返回文本而不是值
$(this).find('select option:selected').each(function (i, val) {
var selectedValue = $(val).val(); //this returns the text from the option instead of the value
});
這是ie7中的一個已知問題嗎?我沒有找到任何有關它的信息?可能的解決方法?由於val()on <option>在ie7中返回文本而不是值
根據該文檔在http://api.jquery.com/val/
.val()
只適用於
的.VAL()方法主要用於獲得的值表單元素 ,比如input,select和textarea。在
<select multiple="multiple">
元素的情況下,.val()方法返回包含每個選定選項的數組 ;如果未選擇任何選項,則返回空值 。
達到你想要什麼,你可以只遍歷select
並呼籲.val()
就可以了,你原來的代碼實際上是呼籲option
val()
,而不是實際select
元素,這就是爲什麼它並未真正發揮作用。
$(this).find('select').each(function (i, val) {
var selectedValue = $(this).val();
});
val()
具有在的情況下multiple
返回值的陣列的能力的一個額外的好處選擇:(強調礦)
的.VAL()方法主要用於獲得表單元素 的值,例如input,select和textarea。 在
<select multiple="multiple">
元素的情況下,.val()方法返回一個數組 ,其中包含每個選定的選項;如果未選擇任何選項,則返回空值 。
嘗試
單選擇
var selectedValue = $(this).val();
你只需要編寫上述行來獲得其他選擇的價值罷了。
爲multislect它應該是
var result = "";
$('#idselect option:selected').each(function(i, item)
{
result += $(this).val() + ", ";
});
不是'$(val)'等價於'$(this)'? – Johan 2012-04-24 10:35:56
我認爲他的意思是你需要的所有代碼。他不是隻取代功能代碼。 – 2012-04-24 10:40:20
在OP的代碼中,'this'(在最外層)顯然不是** select **元素(它包含*一個或多個'select'元素),所以這是行不通的。 – 2012-04-24 10:49:54
這是一個預期的行爲,因爲您要求的是選項的值而不是元素本身。
不要過分複雜的事情。
$(this).find('select').each(function() {
var val = $(this).val();
});
你運行的是什麼版本的jQuery? – Matt 2012-04-24 10:37:29
這不是你應該得到選定值的方式。在'select'元素上調用'.val()'。 – 2012-04-24 10:37:47
Fyi,在'.each()'中,你通常不需要接受任何參數 - 該元素也可以通過'this'使用。 – ThiefMaster 2012-04-24 10:40:41