ITeye博客

JS取下拉框的Text和value值

guodongbingtuan · 2009-09-12 11:57 · JS&AJAX · Markdown

页面jsp:

<select name="qType" id="qType"  onchange="getValAndTxt()">
        <option value="null">---请选择---</option>
        	<c:choose>
		<c:when test="${!empty qtypelist}">
			<c:forEach items="${qtypelist}" var="qtype">
				<c:choose>
					<c:when test="${qtype.qtype_id == qtypeId}">
						<option value="${qtype.qtype_id}" selected="selected">${qtype.qtype_name }</option>
					</c:when>	
					<c:otherwise>
						<option value="${qtype.qtype_id}">${qtype.qtype_name }</option>
					</c:otherwise>					
				</c:choose>				
			</c:forEach>
		</c:when>			
	</c:choose>
        </select>

 js文件:

function getValAndTxt(){

//js取值
	var qtypejs = document.getElementById("qType");
	var index = qtypejs.selectedIndex;
	var valjs = qtypejs.options[index].value;
	var txtjs = qtypejs.options[index].text;
	alert(valjs + " and " + txtjs);
//jQuery取值
	var typeval = $("select[name='qType'] option:selected").val();
	var typetxt = $("select[name='qType'] option:selected").text();
	alert(typeval + "and" + typetxt);
	//var typetxt = $("select[name='qType'] option[selected='selected']").text();//取值为空不知道其中原因
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。