HTML DOM Radio value 属性
Radio value 属性
Radio 对象定义和用法
value 属性可设置或返回单选按钮的 value 属性的值。
对于单选按钮,value属性值的内容不会出现在 用户界面。value 属性值只在提交表单时向服务端传递数据。如果一个 单选按钮处于选中状态,在提交表单时,单选按钮的值才会传递到服务端(未选中按钮是不会传递数据到服务端的)。
语法
设置 value 属性:
radioObject.value="value"
返回 value 属性:
radioObject.value
浏览器支持
所有主要浏览器都支持 value 属性
实例
实例 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>
尝试一下 »
实例 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 对象