【发布时间】:2011-01-14 09:04:13
【问题描述】:
如何使用 jQuery 在select 列表中设置option 值?我试过了:
$('#SelectGroup :all').replace("tmp4", "abc");
我的意思是:搜索 tmp4 字符串并将其替换为 abc。列表中有 4 项我不介意直接进入 #4 入口。
我将扩展我的问题:我想传递给从外部替换值的函数。我试过了:
$('#SelectGroup option').each(function(fieldNum, newVal) {
this.text = this.text.replace('tmp4',$(newVal).text());
});
});
我试过了:
$('#SelectGroup option').each(newVal, function() {
this.text = this.text.replace('tmp4',newVal);
});
});
但这里说它失败了!在.each的第一行
为什么?你有答案吗?
我不明白!我将一个值传递给一个函数,但它在函数中失去了它的值并改变了
var newVal= $(this).val();
$('#SelectGroup option').each(function(fieldNum, newVal) {
alert("final option text is:" + $(this).text());*/
alert($(newVal).text());
this.text = this.text.replace('tmp4',$(newVal).text());
});
});
但是 newVal 不是我传递给函数的——它是 for each 给它的
【问题讨论】: