使用 JavaScript 获取 SelectedValue ASP.NET RadiobuttonList
Use JavaScript to Get SelectedValue ASP.NET RadiobuttonList
我在 .aspx 页面上有以下单选按钮列表:
<asp:RadioButtonList ID="rbList" runat="server">
<asp:ListItem Text="I accept" Value="accept" />
<asp:ListItem Text="I decline" Value="decline" Selected="True" />
</asp:asp:RadioButtonList>
默认选中第二个收音机。有没有办法让我确定用户是否没有选择第一个选项,即,当他们执行操作时,"decline" 是否仍然被选中?
例如:
function checkRbList() {
var rbl = document.getElementById(<%= rbList.ClientID %>);
//if "decline" is still selected, alert('You chose to decline')...
}
以下内容应该可以完成这项工作:
var rbl = document.getElementById("<%= rbList.ClientID %>");
var value = rbl.value;
if(value === 'decline')
alert()
假设你有这个 HTML 呈现:
<label>
I accept
<input id="rbList_0" name="rbList" type="radio" value="accept" />
</label>
<label>
I decline
<input id="rbList_1" name="rbList" checked="true" type="radio" value="decline" />
</label>
您可以使用 document.getElementsByName()
。然后使用:
document.getElementsByName("rbList")
你会得到 NodeList
.
这是函数:
function checkRbList() {
var rbl = document.getElementsByName("rbList"), len = rbl.length;
for (var i = 0; i < len; i++) {
if (rbl[i].checked) { // If checked?
return rbl[i].value; // Returns the selected value.
}
}
}
检查 "decline"
是否仍被选中:
var targetValue = "decline";
if (checkRbList() === targetValue) {
alert("You chose to decline.");
}
像这样:
(function() {
var targetValue = "decline";
function checkRbList() {
var rbl = document.getElementsByName("rbList"),
len = rbl.length;
for (var i = 0; i < len; i++) {
if (rbl[i].checked) { // If checked?
return rbl[i].value; // Returns the selected value.
}
}
}
var btnValidate = document.getElementById("btnValidate");
btnValidate.onclick = function() {
console.log(checkRbList()); // Prints the selected value.
if (checkRbList() === targetValue) {
alert("You chose to decline.");
}
};
})();
<label>
I accept
<input id="rbList_0" name="rbList" type="radio" value="accept" />
</label>
<label>
I decline
<input id="rbList_1" name="rbList" checked="true" type="radio" value="decline" />
</label>
<button id="btnValidate" type="button">Validate</button>
我找到了一个有效的方法:
var targetValue = "decline";
$('#<% = myBtn.ClientID %>').click(function () {
var items = $("#<% = rbList.ClientID %> input:radio");
for (var i = 0; i < items.length; i++) {
if (items[i].value == targetValue) {
if (items[i].checked) {
alert(items[i].value);
}
}
}
});
我在 .aspx 页面上有以下单选按钮列表:
<asp:RadioButtonList ID="rbList" runat="server">
<asp:ListItem Text="I accept" Value="accept" />
<asp:ListItem Text="I decline" Value="decline" Selected="True" />
</asp:asp:RadioButtonList>
默认选中第二个收音机。有没有办法让我确定用户是否没有选择第一个选项,即,当他们执行操作时,"decline" 是否仍然被选中?
例如:
function checkRbList() {
var rbl = document.getElementById(<%= rbList.ClientID %>);
//if "decline" is still selected, alert('You chose to decline')...
}
以下内容应该可以完成这项工作:
var rbl = document.getElementById("<%= rbList.ClientID %>");
var value = rbl.value;
if(value === 'decline')
alert()
假设你有这个 HTML 呈现:
<label>
I accept
<input id="rbList_0" name="rbList" type="radio" value="accept" />
</label>
<label>
I decline
<input id="rbList_1" name="rbList" checked="true" type="radio" value="decline" />
</label>
您可以使用 document.getElementsByName()
。然后使用:
document.getElementsByName("rbList")
你会得到 NodeList
.
这是函数:
function checkRbList() {
var rbl = document.getElementsByName("rbList"), len = rbl.length;
for (var i = 0; i < len; i++) {
if (rbl[i].checked) { // If checked?
return rbl[i].value; // Returns the selected value.
}
}
}
检查 "decline"
是否仍被选中:
var targetValue = "decline";
if (checkRbList() === targetValue) {
alert("You chose to decline.");
}
像这样:
(function() {
var targetValue = "decline";
function checkRbList() {
var rbl = document.getElementsByName("rbList"),
len = rbl.length;
for (var i = 0; i < len; i++) {
if (rbl[i].checked) { // If checked?
return rbl[i].value; // Returns the selected value.
}
}
}
var btnValidate = document.getElementById("btnValidate");
btnValidate.onclick = function() {
console.log(checkRbList()); // Prints the selected value.
if (checkRbList() === targetValue) {
alert("You chose to decline.");
}
};
})();
<label>
I accept
<input id="rbList_0" name="rbList" type="radio" value="accept" />
</label>
<label>
I decline
<input id="rbList_1" name="rbList" checked="true" type="radio" value="decline" />
</label>
<button id="btnValidate" type="button">Validate</button>
我找到了一个有效的方法:
var targetValue = "decline";
$('#<% = myBtn.ClientID %>').click(function () {
var items = $("#<% = rbList.ClientID %> input:radio");
for (var i = 0; i < items.length; i++) {
if (items[i].value == targetValue) {
if (items[i].checked) {
alert(items[i].value);
}
}
}
});