Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping select.change from posting back

this problem is part of a bigger page but I've simplified the code to provide an easy example of what I'm talking about. Basically I have an ASP dropdownlist with AutoPostBack="true" and I want to do some javascript client side which can stop the postback if necessary. However, I can't get it to stop the postback.

<script type="text/javascript">
$(document).ready(function()
{
    $("#ddl").bind('change', function() {
        return false;
    });
});
</script>

<select id="ddl" onchange="form.submit()">
    <option value="">Item 1</option>
    <option value="">Item 2</option>
    <option value="">Item 3</option>
    <option value="">Item 4</option>
    <option value="">Item 5</option>
</select>
like image 238
rpf3 Avatar asked Mar 14 '26 08:03

rpf3


1 Answers

This is my workaround (Firefox works, IE 7/8 works):

$(function() {
    var $select = $('#ddl'), tmp = $select.attr('onchange');

    $select.attr('onchange', '');

    $select.change(function() {
        // If you want to postback:
        tmp();
    });
});

Simple removes the HTML handler and puts it into a variable. Then you can register your change function and if you want to call the auto postback function you can.

like image 70
Bob Fincheimer Avatar answered Mar 16 '26 15:03

Bob Fincheimer



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!