在采取行动的情况下获取单选按钮的选定值
您好,下面是代码,我尝试了很多方法,但无法获得所选单选按钮的值.如您所见,我需要为不同的情况获取该值.
<div style="display: inline-block"><a href="/Login/LogOut">注销</a><span>欢迎 </span>YS User 1 noName </div><br/><br/><br/><br/><table class="MyTable"><thead><tr class="columnHead"><th scope="col"></th><th scope="col"><a href="/User/Index?sort=UserID&sortdir=ASC">CreatedBy</a></th><th scope="col"><a href="/User/Index?sort=CreatedDate&sortdir=ASC">创建日期</a></th><th scope="col"><a href="/User/Index?sort=IsRunning&;amp;sortdir=ASC">正在运行</a></th></tr></thead><tbody><tr><td><输入名称=选择"ID=1"type="radio" value="1"/></td><td>1</td><td>12/12/2011 3:43:57 PM</td><td>假</td></tr><tr class="altRow"><td><输入名称=选择"ID=2"type="radio" value="2"/></td><td>1</td><td>12/13/2011 4:42:37 PM</td><td>假</td></tr><tr><td><输入名称=选择"ID=3"type="radio" value="3"/></td><td>1</td><td>12/13/2011 6:27:38 PM</td><td>错误的</td></tr></tbody></table></div></div></身体></html><脚本类型="文本/javascript">//var value = $$('input[name=selected]:checked')[0].get('value');//变量选择Foo;//$$('input[name=selected]').each(function (el) {//if (el.checked == true) {//selectFoo = el.value;//}//});函数 getCheckedValue(radioObj) {如果(!radioObj)返回 "";var radioLength = radioObj.length;如果(radioLength == 未定义)如果(radioObj.checked)返回radioObj.value;别的返回 "";for (var i = 0; i < radioLength; i++) {if (radioObj[i].checked) {返回 radioObj[i].value;}}返回 "";};函数提交创建(){var adress = "/User/CreateSurvey/";document.location = 地址;};函数提交编辑(){var adress = "/Den/Index/" + getCheckedValue('selected');document.location = 地址;};函数提交删除(){var adress = "/User/DeleteSurvey/" + getCheckedValue('selected');document.location = 地址;};函数提交预览(){var adress = "/User/PreviewSurvey/" + getCheckedValue('selected');document.location = 地址;};</脚本>
解决方案 你可以使用 document.getElementsByName(<button_name>)
或 document.getElementsByTagName("input")
获取输入元素的数组.循环遍历这些元素以检查哪些是 checked
.
下面是一个示例,说明如何从一组名为selected"的单选按钮中获取选中按钮的值:
<html><头><脚本类型="文本/javascript">函数 get_radio_value() {var 输入 = document.getElementsByName("selected");for (var i = 0; i < inputs.length; i++) {如果(输入[i].checked){返回输入[i].value;}}}函数 onSubmit() {var id = get_radio_value();alert("选择的输入是:" + id);}</脚本></头><身体><form onsubmit="onSubmit();"><input name="selected" value="1" type="radio"/>1<br/><input name="selected" value="2" type="radio"/>2<br/><input name="selected" value="3" type="radio"/>3<br/><输入类型="提交" 值="提交"/></表格></身体></html>
Hi here is the codes bellow, I've tried many things but I couldnt get value of selected radio button.As you can see I need to get that value for diffrent situations.
<div style="display: inline-block">
<div>
<a href="/Login/LogOut">Log Out</a><span> Welcome </span>YS User 1 noName </div>
<br />
<br />
<br />
<br />
<input type="button" onclick="submitCreate();" value="New Survey" /><br />
<input type="button" onclick="submitEdit();" value="Edit Survey" /><br />
<input type="button" onclick="submitDelete();" value="Delete Survey" /><br />
<input type="button" onclick="submitPreview();" value="Preview Survey" />
</div>
<div style="display: inline-block">
<div>
<table class="MyTable"><thead><tr class="columnHead"><th scope="col"></th><th scope="col"><a href="/User/Index?sort=UserID&sortdir=ASC">CreatedBy</a></th><th scope="col"><a href="/User/Index?sort=CreatedDate&sortdir=ASC">Created Date</a></th><th scope="col"><a href="/User/Index?sort=IsRunning&sortdir=ASC">Is Running</a></th></tr></thead><tbody><tr><td> <input name="selected" id="1"
type="radio" value="1" /></td><td>1</td><td>12/12/2011 3:43:57 PM</td><td>False</td></tr><tr class="altRow"><td> <input name="selected" id="2"
type="radio" value="2" /></td><td>1</td><td>12/13/2011 4:42:37 PM</td><td>False</td></tr><tr><td> <input name="selected" id="3"
type="radio" value="3" /></td><td>1</td><td>12/13/2011 6:27:38 PM</td><td>False</td></tr></tbody></table>
</div>
</div>
</body>
</html>
<script type="text/javascript">
// var value = $$('input[name=selected]:checked')[0].get('value');
// var selectFoo;
// $$('input[name=selected]').each(function (el) {
// if (el.checked == true) {
// selectFoo = el.value;
// }
// });
function getCheckedValue(radioObj) {
if (!radioObj)
return "";
var radioLength = radioObj.length;
if (radioLength == undefined)
if (radioObj.checked)
return radioObj.value;
else
return "";
for (var i = 0; i < radioLength; i++) {
if (radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
};
function submitCreate() {
var adress = "/User/CreateSurvey/";
document.location = adress;
};
function submitEdit() {
var adress = "/Den/Index/" + getCheckedValue('selected');
document.location = adress;
};
function submitDelete() {
var adress = "/User/DeleteSurvey/" + getCheckedValue('selected');
document.location = adress;
};
function submitPreview() {
var adress = "/User/PreviewSurvey/" + getCheckedValue('selected');
document.location = adress;
};
</script>
解决方案
You can use document.getElementsByName(<button_name>)
or document.getElementsByTagName("input")
to get an array of input elements. Loop through those elements to check which is checked
.
Here is an example of how to get the value of the checked button from a set of radio buttons with the name "selected":
<html>
<head>
<script type="text/javascript">
function get_radio_value() {
var inputs = document.getElementsByName("selected");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
return inputs[i].value;
}
}
}
function onSubmit() {
var id = get_radio_value();
alert("selected input is: " + id);
}
</script>
</head>
<body>
<form onsubmit="onSubmit();">
<input name="selected" value="1" type="radio"/>1<br/>
<input name="selected" value="2" type="radio"/>2<br/>
<input name="selected" value="3" type="radio"/>3<br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
相关文章