Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get radio button choice value in Ajax Behavior

How can I get radio button choice "live" in wicket? I mean if I've a radio button with two choices A and B and I want to add an ajax behavior (on change, on update, whatever works) to the button so that if I chose A it happens something in the page, if I chose B happens something else. Can You help me?

like image 791
andPat Avatar asked Oct 27 '25 20:10

andPat


1 Answers

Just add an AjaxFormChoiceComponentUpdatingBehavior to the RadioChoice:

RadioChoice radioChoice = new RadioChoice("myRadio", choiceList);
radioChoice.add( new AjaxFormChoiceComponentUpdatingBehavior("onchange") { 
    protected void onUpdate(AjaxRequestTarget target) {
        // Ajax actions here
        System.out.println("The selected value is " + getComponent().getDefaultModelObjectAsString()));
    }
}  );
form.add(radioChoice);

The Model of the RadioChoice will already be updated when executing onUpdate. Remember to add to target all components that are to be refreshed via AJAX (and of course to set setOutputMarkupId(true) to them).

There's an online example with source code of what you're looking for here. The relevant source code file is this one.

like image 140
Xavi López Avatar answered Oct 30 '25 10:10

Xavi López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!