javascript - How can I cancel a users radiobutton selection, after a confirm dialog cancel selection? - Stack Overflow

admin2025-04-17  0

In a bigger project, I have a group of radiobuttons, which updates the text in a textarea. Sometimes however, I need to let the user choose if they really want to change the text.

I'm using hidden radiobuttons, and only the labels are showing. I've been able to present the confirm dialog, and prevent the change of text in the textarea, but I have not been able to actually stop the radiobutton from checking the newly clicked element. I've tried to save a reference to the previously checked radiobutton, but I suspect that it is already to late in the code. To further illustrate, I have made a JSFiddle. There should never be a mismatch between button texts and text in the area, once one has been loaded.

/

So is there a way to cancel the user's selection of the radiobutton?

HTML:

<nav id="selectors" class="segmented-button">
    <input type="radio" name="group-1" value="car" id="radio1">
    <label for="radio1" class="first">Car</label> 
    <input type="radio" name="group-1" value="boat" id="radio2">
    <label for="radio2">Boat</label> 
    <input type="radio" name="group-1" value="plane" id="radio3">
    <label for="radio3">Plane</label> 
    <input type="radio" name="group-1" value="train" id="radio4">
    <label for="radio4" class="last">Train</label> 
</nav>

    <textarea id="output">output area</textarea>

javascript:

 $(document).ready(function(){
    $('#selectors label').click(function() {    
        var element = $(this).prev();
        var confirmed = false;
        var previousSelected =  $("input[name=group-1]:checked");
        var r = confirm("Are you sure you want to change the text?");
        if (r == true) {
            $("#output").val(element.val());

        } else{
            previousSelected.checked = true;
        }
    });
});

In a bigger project, I have a group of radiobuttons, which updates the text in a textarea. Sometimes however, I need to let the user choose if they really want to change the text.

I'm using hidden radiobuttons, and only the labels are showing. I've been able to present the confirm dialog, and prevent the change of text in the textarea, but I have not been able to actually stop the radiobutton from checking the newly clicked element. I've tried to save a reference to the previously checked radiobutton, but I suspect that it is already to late in the code. To further illustrate, I have made a JSFiddle. There should never be a mismatch between button texts and text in the area, once one has been loaded.

http://jsfiddle/e0bxnghc/1/

So is there a way to cancel the user's selection of the radiobutton?

HTML:

<nav id="selectors" class="segmented-button">
    <input type="radio" name="group-1" value="car" id="radio1">
    <label for="radio1" class="first">Car</label> 
    <input type="radio" name="group-1" value="boat" id="radio2">
    <label for="radio2">Boat</label> 
    <input type="radio" name="group-1" value="plane" id="radio3">
    <label for="radio3">Plane</label> 
    <input type="radio" name="group-1" value="train" id="radio4">
    <label for="radio4" class="last">Train</label> 
</nav>

    <textarea id="output">output area</textarea>

javascript:

 $(document).ready(function(){
    $('#selectors label').click(function() {    
        var element = $(this).prev();
        var confirmed = false;
        var previousSelected =  $("input[name=group-1]:checked");
        var r = confirm("Are you sure you want to change the text?");
        if (r == true) {
            $("#output").val(element.val());

        } else{
            previousSelected.checked = true;
        }
    });
});
Share asked Aug 8, 2014 at 15:53 jumps4funjumps4fun 4,13010 gold badges54 silver badges100 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Returning false in the else should do it:

else{
        previousSelected.checked = true;
        return false;
}

jsFiddle example

Simply returning 'false' from the radio button click event rolls back the action. We don't need to save the previously selected radio button. Browser does this for us:

$(document).ready(function () {
        $('input[name=Group]').click(function () {
            var confirmation = confirm('Are you sure you want to change the default message?');
            if (!confirmation) {
                return false;
            }
        })
    });
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744884295a272444.html

最新回复(0)