jQuery click event on radio button doesn't get fired

后端 未结 6 853
栀梦
栀梦 2021-01-01 14:27

I\'ve got the following code to trigger a click event on some radio buttons! but it doesn\'t get fired! can any one help me with this!

CODE :

6条回答
  •  滥情空心
    2021-01-01 15:06

    There are a couple of things wrong in this code:

    1. You're using the wrong way. You should use a if you want to make the text behind it clickable.
    2. It's setting the enabled attribute, which does not exist. Use disabled instead.
    3. If it would be an attribute, it's value should not be false, use disabled="disabled" or simply disabled without a value.
    4. If checking for someone clicking on a form event that will CHANGE it's value (like check-boxes and radio-buttons), use .change() instead.

    I'm not sure what your code is supposed to do. My guess is that you want to disable the input field with class roomNumber once someone selects "Walk in" (and possibly re-enable when deselected). If so, try this code:

    HTML:

    Javascript:

    $("form input:radio").change(function () {
        if ($(this).val() == "walk_in") {
            // Disable your roomnumber element here
            $('.roomNumber').attr('disabled', 'disabled');
        } else {
            // Re-enable here I guess
            $('.roomNumber').removeAttr('disabled');
        }
    });
    

    I created a fiddle here: http://jsfiddle.net/k28xd/1/

提交回复
热议问题