jquery - Select all other fields in a form -


i looking how can disable other inputs on form when checkbox selected. have halfway there now, when problem when click checkbox, disables along other elements. want other elements become disabled.

http://jsfiddle.net/rockitdev/8tf2a/3/

    <form action="#" method="post">  <div>     <label for="checkbox">checkbox:</label>     <input type="checkbox" name="checkbox" id="checkbox" /> </div>  <div>      <label for="name">text input:</label>      <input type="text" name="name" id="name" value="" tabindex="1" /> </div>  <div>      <h4>radio button choice</h4>       <label for="radio-choice-1">choice 1</label>      <input type="radio" name="radio-choice-1" id="radio-choice-1" tabindex="2" value="choice-1" />       <label for="radio-choice-2">choice 2</label>      <input type="radio" name="radio-choice-2" id="radio-choice-2" tabindex="3" value="choice-2" /> </div>  <div>     <label for="select-choice">select dropdown choice:</label>     <select name="select-choice" id="select-choice">         <option value="choice 1">choice 1</option>         <option value="choice 2">choice 2</option>         <option value="choice 3">choice 3</option>     </select> </div>  <div>     <label for="textarea">textarea:</label>     <textarea cols="40" rows="8" name="textarea" id="textarea"></textarea> </div>    <div>     <input type="submit" value="submit" /> </div> 

$('#checkbox').click(function () {     var $this = $(this);     // $this contain reference checkbox        if ($this.is(':checked')) {         $('input:not(this)').attr("disabled", true);     } else {      } }); 

$('#checkbox').change(function(e){   var $form = $(this).closest('form'); // locate form we're referring   // alternative method: var $form = $(this.form);          $form.find(':input') // grab input elements (buttons, inputs, select lists, etc.)     .not(this) // exclude checkbox     .attr('disabled', !this.checked); // mark these elements disabled                                       // based on checkbox value }); 

use .not() exclude current element in selection. , recommend binding change instead of click in case user decides tab through form.

also, jsfiddle (though in demo reference this.form off input element, can retain $form in example above, too.)


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -