`
yuyee
  • 浏览: 57568 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

javascript中select操作

阅读更多
几个对象的获取
var obj = document.getElementById(”select_id”); //selectid

var index = obj.selectedIndex; // 选中索引

var text = obj.options[index].text; // 选中文本

var value = obj.options[index].value; // 选中值


小操作,玩玩
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>

<script type="text/javascript">
function getResult(){
var obj=document.getElementById("province").options;
for(var i=0;i<obj.length;i++){
alert(obj[i].text);
}
alert("11");
var username=document.getElementById("username");
var password=document.getElementById("pwd");
if(username.value=="" && password.value==""){
alert("空");
}
}
</script>
</head>
<body>
<form name="f" onsubmit="getResult();">
    <fieldset>
        <legend>用户注册</legend>
        <div>
            <label for="username">帐号</label>
            <input type="text" id="username" name="username" />
        </div>
        <div>
            <label for="pwd">密码</label>
            <input type="password" name="pwd" id="pwd" />
        </div>
        <div>
            <label for="province">省份</label>
            <select id="province" name="province" >
                <option value="10">江西</option>
                <option value="11">福建</option>
                <option value="12">广东</option>
                <option value="13">浙江</option>
            </select>
        </div>
    </fieldset>
    <input type="submit" value="提交" name="btnSub" />
</form>
</body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics