javascript - jQuery, how to disable or enable <select> element with checkbox upon page loading? - Stack Overflow

I have a simple function which disablesenables select element when checkbox is checkedunchecked. It w

I have a simple function which disables/enables select element when checkbox is checked/unchecked. It works well. However, once the form is submitted and the checkbox loads already checked (with help of PHP), selection is not disabled until I click on the checkbox twice.

<select id="worldSelect" class="select" name="world">
<input id="worldcb" type="checkbox" checked="yes" value="any" name="world">
any world 

function toggleSelection(element){
    if (this.checked) {
        $(element.data).attr('disabled', 'disabled');
    } else {
        $(element.data).removeAttr('disabled');
    }
}

$(function() {
    $('#worldcb').click('#worldSelect', toggleSelection);
});

I have tried several things, however I'm not any good with jQuery so far...

I have a simple function which disables/enables select element when checkbox is checked/unchecked. It works well. However, once the form is submitted and the checkbox loads already checked (with help of PHP), selection is not disabled until I click on the checkbox twice.

<select id="worldSelect" class="select" name="world">
<input id="worldcb" type="checkbox" checked="yes" value="any" name="world">
any world 

function toggleSelection(element){
    if (this.checked) {
        $(element.data).attr('disabled', 'disabled');
    } else {
        $(element.data).removeAttr('disabled');
    }
}

$(function() {
    $('#worldcb').click('#worldSelect', toggleSelection);
});

I have tried several things, however I'm not any good with jQuery so far...

Share Improve this question asked May 9, 2012 at 21:53 Paweł DudaPaweł Duda 1,7834 gold badges18 silver badges36 bronze badges 3
  • Are you POSTing the form or submitting via AJAX? Can't you uncheck it by default on pageload? Maybe add the code you're using to submit the form as well. – jamesmortensen Commented May 9, 2012 at 21:56
  • Dupe of stackoverflow.com/questions/10523620/… ? – SenorAmor Commented May 9, 2012 at 22:01
  • no, not really... My previous problem was fixed, however another one that came was I wanted to get it to work on page load – Paweł Duda Commented May 10, 2012 at 7:51
Add a comment  | 

4 Answers 4

Reset to default 9

Looks like you were almost there. Just a couple of syntax errors.

$("#worldcb").click(function(){
    var el = $("#worldSelect");
    if (el){
        el.removeAttr("disabled");
        if (this.checked){
            el.attr("disabled", "disabled");     
        }
    }
});        

Here's a jsFiddle to demonstrate.

As for retaining the disabled state when the page is posted back, you just need to run some logic when the page is loaded.

if ($("#worldcb").is(":checked")){
    $("#worldSelect").attr("disabled", "disabled");
}

Well easiest way would be asigning your toggleFunction to document ready, so every time page loads it will check that checkbox status, and correct displaying of the select. You can give autocomplete="off" atribute to specific form field, to make it not cache (so the checkbox will be turned off after page refresh).

Example:

$(document).ready(function() {
    toggleSelection('#worldcb');
});

Well you need to sync the two on load. Also change toggleSelection so that it works both called as an event handler or standalone

function toggleSelection( element, scope ){
    scope = scope || this;
    if (scope.checked) {
        $(element.data).attr('disabled', 'disabled');
    } else {
        $(element.data).removeAttr('disabled');
    }
}

$(function() {
    $('#worldcb').click('#worldSelect', toggleSelection);
    toggleSelection( { data : '#worldSelect' }, document.querySelector( '#worldcb' ) );
});

If you always want it to be disabled when the page loads, just add the disabled property to the select html element.

<select id="worldSelect" class="select" name="world" disabled></select>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1739444897a4127581.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信