我要设置选择框的选定值(如果存在),如果不存在,则将其添加到选择框。
我怎么做?
我有:
<select id="abc" name="abc">
</select>
我想:
$('#abc').val(123);
然后:
<select id="abc" name="abc">
<option value="123" selected>
</select>
下面的代码怎么样。
if($('#abc option[value=' + testValue + ']').length === 0) {
$('#abc').append('<option value="' + testValue + '">' + testValue + '</option>');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句