jQuery click event on radio button doesn't get fired

后端 未结 6 810
栀梦
栀梦 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 14:53

    put ur js code under the form html or use $(document).ready(function(){}) and try this.

    $('#inline_content input[type="radio"]').click(function(){
                    if($(this).val() == "walk_in"){
                        alert('ok');
                    }
    });
    
    0 讨论(0)
  • 2021-01-01 14:56

    It fires. Check demo http://jsfiddle.net/yeyene/kbAk3/

    $("#inline_content input[name='type']").click(function(){
        alert('You clicked radio!');
        if($('input:radio[name=type]:checked').val() == "walk_in"){
            alert($('input:radio[name=type]:checked').val());
            //$('#select-table > .roomNumber').attr('enabled',false);
        }
    });
    
    0 讨论(0)
  • 2021-01-01 14:56

    Seems like you're #inline_content isn't there! Remove the jQuery-Selector or check the parent elements, maybe you have a typo or forgot to add the id.

    (made you a jsfiddle, works after adding a parent <div id="inline_content">: http://jsfiddle.net/J5HdN/)

    0 讨论(0)
  • 2021-01-01 15:06

    There are a couple of things wrong in this code:

    1. You're using <input> the wrong way. You should use a <label> 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:

    <form class="type">
        <p>
            <input type="radio" name="type" checked="checked" id="guest" value="guest" />
            <label for="guest">In House</label>
        </p>
        <p>
            <input type="radio" name="type" id="walk_in" value="walk_in" />
            <label for="walk_in">Walk in</label>
        </p>
        <p>
            <input type="text" name="roomnumber" class="roomNumber" value="12345" />
        </p>
    </form>
    

    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/

    0 讨论(0)
  • 2021-01-01 15:11

    A different way

    $("#inline_content input[name='type']").change(function () {
        if ($(this).val() == "walk_in" && $(this).is(":checked")) {
            $('#select-table > .roomNumber').attr('enabled', false);
        }
    });
    

    Demo - http://jsfiddle.net/cB6xV/

    0 讨论(0)
  • 2021-01-01 15:19

    Personally, for me, the best solution for a similar issue was:

    HTML

    <input type="radio" name="selectAll" value="true" />
    <input type="radio" name="selectAll" value="false" />
    

    JQuery

    var $selectAll = $( "input:radio[name=selectAll]" );
        $selectAll.on( "change", function() {
             console.log( "selectAll: " + $(this).val() );
             // or
             alert( "selectAll: " + $(this).val() );
        });
    

    *The event "click" can work in place of "change" as well.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题