Radio value 屬性
Radio 對象定義和用法
value 屬性可設(shè)置或返回單選按鈕的 value 屬性的值。
對于單選按鈕,value屬性值的內(nèi)容不會(huì)出現(xiàn)在 用戶界面。value 屬性值只在提交表單時(shí)向服務(wù)端傳遞數(shù)據(jù)。如果一個(gè) 單選按鈕處于選中狀態(tài),在提交表單時(shí),單選按鈕的值才會(huì)傳遞到服務(wù)端(未選中按鈕是不會(huì)傳遞數(shù)據(jù)到服務(wù)端的)。
語法
設(shè)置 value 屬性:
radioObject.value="value"
返回 value 屬性:
radioObject.value
瀏覽器支持
所有主要瀏覽器都支持 value 屬性
實(shí)例
實(shí)例 1
顯示 "Red" 單選按鈕的值:
<html>
<head>
<script>
function displayResult()
{
var x=document.getElementById("red").value;
alert(x);
}
</script>
</head>
<body>
<form>
What color do you prefer?<br>
<input type="radio" name="colors" id="red" value="red">Red<br>
<input type="radio" name="colors" id="blue" value="blue">Blue<br>
<input type="radio" name="colors" id="green" value="green">Green
</form>
<button type="button" onclick="displayResult()">Display value</button>
</body>
</html>
<head>
<script>
function displayResult()
{
var x=document.getElementById("red").value;
alert(x);
}
</script>
</head>
<body>
<form>
What color do you prefer?<br>
<input type="radio" name="colors" id="red" value="red">Red<br>
<input type="radio" name="colors" id="blue" value="blue">Blue<br>
<input type="radio" name="colors" id="green" value="green">Green
</form>
<button type="button" onclick="displayResult()">Display value</button>
</body>
</html>
嘗試一下 ?
實(shí)例 2
顯示選定的單選按鈕值:
<html>
<head>
<script>
function displayResult(browser)
{
document.getElementById("result").value=browser
}
</script>
</head>
<body>
<p>Select your favorite browser:</p>
<form>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Internet Explorer">Internet Explorer<br>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Firefox">Firefox<br>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Opera">Opera<br>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Google Chrome">Google Chrome<br>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Safari">Safari<br><br>
Your favorite browser is: <input type="text" id="result">
</form>
</body>
</html>
<head>
<script>
function displayResult(browser)
{
document.getElementById("result").value=browser
}
</script>
</head>
<body>
<p>Select your favorite browser:</p>
<form>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Internet Explorer">Internet Explorer<br>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Firefox">Firefox<br>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Opera">Opera<br>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Google Chrome">Google Chrome<br>
<input type="radio" name="browser" onclick="displayResult(this.value)" value="Safari">Safari<br><br>
Your favorite browser is: <input type="text" id="result">
</form>
</body>
</html>
嘗試一下 ?
Radio 對象
更多建議: