我正在使用jquery 1.3.2,并且在situatsuin中遇到问题,我通过getJson填充select
html:
<div id="dialDiv">
<select id="select_elem" name="select_elem" style="font-size: 1.4em; line-height: 1em; font-weight: bold; color: #333;">
<input type="button" name="lock" id="lock" value="Lock" onclick="LockTime();" />
</div>
javascript:
//通过getJson
填充select_elemfunction ShowDial(){
$.getJSON('GetFreeTimeSlots',{hour:4},function(data)
{
$.each(data, function()
{
$("#select_elem").append("<option value='"+this.Value+"'>" + this.Value + "</option>");
});
}
$("dialDiv").dialog(.......).show();
}
在这个dialDiv里面有一个带onClick事件的按钮:
function LockTime() {
alert($("#select_elem").val());
}
在IE中,此警报显示所选项目值,在chrome中显示“undefined”,并且在ff中它显示select中的第一个选项,因此它仅在ie = /
中正常工作有什么方法可以解决这个问题吗?
谢谢!
答案 0 :(得分:1)
试
var sel_val = $("#select_elem option:selected");
alert(sel_val.val());
建议更好的编码
$.each(data, function(i,v){
$('<option>').val(v).text(v).appendTo("#select_elem")
});
答案 1 :(得分:1)
问题涉及缺席明确指定所选项目。 而其他一点 - 只是建议 - 而不是文本演示使用DOM构造, 这样:
function(){
var oOption = document.createElement("OPTION");
oOption.text=this.Value;
oOption.value=this.Value;
if( some_criteria )//add criteria to select
oOption.selected = true;
$("#select_elem").add(oOption);
}