【问题标题】:Error in setting alert in radiobuttonlist?在单选按钮列表中设置警报时出错?
【发布时间】:2015-09-25 19:45:50
【问题描述】:

我有一个带有单选按钮列表的两个文本框。如果两个测试为空白,则应提醒“文本框为空白”,如果不为空白,则应提醒“日期已启用”。

这是我用 javascript 尝试过的

 function checked() {
        var radio = document.getElementById('<%=rbtn1.ClientId%>');
        var cal1 = document.getElementById('<%=textstqo.ClientId%>').value;
        var cal2 = document.getElementById('<%=textedqo.ClientId%>').value;
    if (radio.checked = true) {

         if(cal1 == '' && cal2 =='')
        {
        alert("dates cannot be blank");
        return false
        }else
        {
        alert("dates are enabled");
         return true
        }
        }

    }

单选按钮列表

<asp:RadioButtonList ID="rbtn1" runat="server" RepeatDirection="Vertical" RepeatLayout="Table" Height="300px" Width="101px" onClick="checked()" >
 <asp:ListItem Text="one" Value="one" Enabled="true"></asp:ListItem>
 <asp:ListItem Text="two" Value="two" Enabled="true" ></asp:ListItem>
<asp:ListItem Text="three" Value="three" Enabled="true"></asp:ListItem>

已编辑 错误:预期功能

第一个警报正在工作。当我尝试在其他部分检查警报时,它显示上述错误 我只向您展示了第一个单选按钮“quaterone”,而 javascript 也适用于 quaterone

谁能告诉我我做错了什么。提前谢谢

【问题讨论】:

  • 您是否尝试过,如果选中任何一个单选按钮,则验证文本框,否则将其保留?
  • no.. 仅当单击第一个单选按钮“quaterone”@
  • @shanish:no.. 仅当单击第一个单选按钮“quaterone”时
  • // when i tried to check the alert in else part it shows the above error I have only shown you the first radio button that is "quaterone" and the javascript is also for quaterone.// 你到底在哪里得到错误。是否来自您问题中给出的来源?
  • @shanish:问题是我只希望在单选按钮列表中的第一个单选按钮(quaterone)被选中时发出警报。但是当我点击另一个单选按钮时它也会显示警报

标签: javascript radiobuttonlist


【解决方案1】:

问题是您为单选按钮使用了错误的 ID。 '&lt;%=rbtn1.ClientId%&gt;_Quaterone'

改用:'&lt;%=rbtn1.ClientId%&gt;'

已编辑:

var radioCont = document.getElementById('<%=rbtn1.ClientId%>');
var radio = radioCont.getElementsByTagName('input')[0];//first radio

您还试图分配价值而不是比较:

if (radio.checked = true) { //assignment

试试这个:

if (radio.checked == true) { // though `===` is recommended over `==`

你也可以试试:

if(radio.checked) {
  // to do here
}

在此处将属性更改为onchange 而不是onclick:

onClick="checked()" >

为了取消选中无线电,为其状态分配 false:

alert("dates cannot be blank");
radio.checked = false; //un-check the radio
return false;

【讨论】:

  • 它工作了@Aravind ...但它显示了第一个警报但是当我尝试检查其他部分时它显示 fuctoin expexted
  • 当我尝试了这两种方法时它没有显示任何 alert@Arvind
  • arvind:问题是我只想在单选按钮列表中的第一个单选按钮(quaterone)被选中时发出警报。但是当我点击另一个单选按钮时它也会显示警报
  • arvind:谢谢它的工作原理:-)...另一个问题是,在出现“不能为空白”的警报后,是否可以取消选中选定的收音机。
【解决方案2】:

要仅在选中第一个单选按钮时进行验证,您可以像这样进行检查

if($('#rbtn1 input:radio:checked').val() == 'quaterone'){
    // your condition here...
}

【讨论】:

    猜你喜欢
    • 2018-02-25
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多