javascript - 在 RadioButton 列表单击事件上显示确认消息框

标签 javascript jquery asp.net

我在 Div 中有一个单选按钮列表(radio1、radio2、radio3)。

当我单击其中一个单选按钮时,我会隐藏并显示一个包含一些复选框的 div。 单击单选按钮时,我需要确保取消选中已选中的复选框。

因此,用户一次可以选择一个单选按钮,并且可以选中与 Div 相关的复选框。 如果用户选择 radio2,那么我应该能够取消选中除 radio2 之外的其他 div 的所有内容。

我能够使用以下代码实现上述场景。

但问题是: - 假设如果 radio2 已经被选中,并且如果用户再次单击 radio2:则取消选中 radio2 的内容。我需要停止。

我想添加一个确认消息框(确定和取消按钮),每次单击它都会警告用户。如果用户单击“取消”,则应将状态保持为现有状态。

我为此编写了一个函数,但它不起作用。

<div id="radioDiv">                    
    <asp:RadioButtonList ID="radioList" runat="server" >
        <asp:ListItem Value="0" >NewYork</asp:ListItem>
        <asp:ListItem Value="1" >London</asp:ListItem>
        <asp:ListItem Value="2" >Paris</asp:ListItem>
    </asp:RadioButtonList>
</div> 

<div id=nyDiv>
    <asp:CheckBox ID="nycheck1" runat="server" ></asp:CheckBox>
        <asp:CheckBox ID="nycheck2" runat="server" ></asp:CheckBox>
</div>
<div id=ldDiv>
    <asp:CheckBox ID="ldcheck1" runat="server" ></asp:CheckBox>
        <asp:CheckBox ID="ldcheck2" runat="server" ></asp:CheckBox>
</div>
<div id=paDiv>
    <asp:CheckBox ID="pacheck1" runat="server" ></asp:CheckBox>
        <asp:CheckBox ID="pacheck2" runat="server" ></asp:CheckBox>
</div>



function showCity(city) {
    jQuery('#radioDiv input').click(function () {
        //Show confirm message  
        showConfirmMessage('#radioDiv input:radio:checked');

        //Show Hide div
        if (jQuery('#radioDiv input:radio:checked').val() == ) {
            jQuery('#nyDiv').css("display", "block");
            jQuery('#ldDiv').css("display", "none");
            jQuery('#paDiv').css("display", "none");
        }
    }
}


function showConfirmMessage(radioDiv) {
    if (jQuery(radioDiv).val() == 0) {
        var result = confirm("Moving to another city will result in loosing all the existing changes.");
        if (result) {
            return true;
        } else {
            return false;
        }
    }
}

最佳答案

所以你是说,当单击一个单选按钮并出现其相应的复选框 div 时,再次单击该单选按钮会将这些复选框重置回默认值。

如果是这种情况,那么您不能添加一个“事件”类或类似的东西来将单选按钮标记为在第一次单击后正在使用中。然后,当您再次单击它时,在重置复选框之前检查该单选按钮是否具有事件类别。

所以基本上,如果用户尝试再次单击该单选按钮,只需禁用该单选按钮,并在单击另一个单选按钮时重新启用它。

请原谅伪代码,我在公交车站。我回家后会举一个例子。

编辑

http://jsfiddle.net/1L34rm96/8/

这只是我认为你正在做的事情的简化版本。转换它应该不会太难。我在 fiddle 中留下了很多评论。

HTML

<form>
<input class="radios" type="radio" name="r" value="male">r1</input>
<br/>
<input class="radios" type="radio" name="r" value="female">r2</input>
<br/>
<input class="radios" type="radio" name="r" value="male">r3</input>
<br/>
</form>
<div id="checkBoxes">
<input type="checkbox" name="vehicle" value="Bike">I have a bike</input>
<br/>
<input type="checkbox" name="vehicle" value="Car">I have a car</input>
<br/>
<input type="checkbox" name="vehicle" value="Bike">I have a wife</input>
<br/>
<input type="checkbox" name="vehicle" value="Car">I have a stupid fat dog</input>
</div>

JS

$(".radios").on("click", function () {
    if (!$this.hasClass("active")) {
        $(".radios").removeClass("active");
        $this.addClass("active");
        $("#checkBoxes").show();
        alert("I'm not selected, get me check boxes!");
    }else{ 
        //the checkboxes for that radio are currently being displayed
        //don't reset your checkboxes!
        //whatever else needs to be here
        alert("I'm already selected, do nothing!");
    }
});

关于javascript - 在 RadioButton 列表单击事件上显示确认消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25489931/

相关文章:

asp.net - 由于 FIPS 140 安全策略,ASPX 页面失败

JavaScript 'var' 数据/对象大小

javascript - 根据其他输入值将复选框设置为选中

jquery - 如何使用 npm 加载 bootstrap?

javascript - 使用 jQuery 添加选择字段的选项

asp.net - mvc : does the favicon. ico 还寻找 Controller ?

c# - GridView 不在 GridView_PageIndexChanging 上显示

javascript - chrome 中的页面闪烁 - bootstrap 和 jquery 冲突

php - 我如何获得任何浏览器的 javascript 状态?

c# - 寻找一种将视频上传到带有jQuery的youtube而不回发的方法