Determining selected state of jQuery Buttons

Posted by lloydphillips on Stack Overflow See other posts from Stack Overflow or by lloydphillips
Published on 2010-06-01T01:52:45Z Indexed on 2010/06/01 5:03 UTC
Read the original article Hit count: 190

Filed under:
|
|

I've got two radio buttons in a .net page which are being transformed to jQuery buttons a la http://jqueryui.com/demos/button/#radio

When the page is loaded I have button 2 as checked. When clicking the buttons I'm firing the postback event. Problem is you can click on that button that is selected by default on the initial load i.e. Button 2, the postback is fired but the event handler isn't called in the .net code behind because the radio button is already classed as selected (and in normal circumstances wouldn't allow the postback to fire).

To get around this I've added the e.PreventDefault() method BUT this is causing issues when Button 1 is clicked because before the click handler is called the button is set to selected. Therefore, in every case in the following code e.PreventDefault() is called:

$(document).ready(function(){
    $("[id*='rbPayable']").click(function(e){
        if ($("[id*='rbPayable']").attr("checked"))
            e.preventDefault();
        else
            setTimeout('__doPostBack(\'this.id\',\'\')', 0)
    })
    $("[id*='rbReceivable']").click(function(e){
        if ($("[id*='rbReceivable']").attr("checked"))
            e.preventDefault();
        else
            setTimeout('__doPostBack(\'this.id\',\'\')', 0)
    })
});

What is the best way for me to load the page and effectively be able to do the following: 'If rbReceivable is checked then don't do anything otherwise do a postback.'

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about jQuery