使用document.getElementbyID()方法获取所选选项ID

时间:2013-11-01 10:25:17

标签: javascript jquery

我希望在javascript中选择选项ID,

personalData['childname']=window.frames[0].document.getElementById('student_name').value;

HTML ::

<tr>
<td align="right" width="40%"><label for="student" class="student" data-icon="">Select Name</label></td>
<td><select class="field" id="student_name"  required="required"  >

</select></td>
</tr>

**动态加载值**

此行给出了值,如何获得所选选项的id ???

4 个答案:

答案 0 :(得分:1)

未经测试

var $select = $('#student_name', window.frames[0].document);
var id = $select.find('[value="' + $select.val() + '"]').attr('id');

var id = $('#student_name option:selected', window.frames[0].document).attr('id');

答案 1 :(得分:0)

试试这个:

personalData['childname']=window.frames[0].document.getElementById('student_name').getAttribute("id");

答案 2 :(得分:0)

尝试

javascript:

personalData['childname']=window.frames[0].document.getElementById('student_name').id;

jQuery:

personalData['childname']=$('#student_name option:selected', window.frames[0].document).attr('id');

答案 3 :(得分:0)

JS

var elem = window.frames[0].document.getElementById('student_name');
personalData['childname'] = elem.options[elem.selectedIndex].id;

请参阅jsfiddle Demo