Friday, December 29, 2017
javascript Using jQuery to change a dropdown menu when a checkbox is checked
javascript Using jQuery to change a dropdown menu when a checkbox is checked
javascript - Using jQuery to change a dropdown menu when a checkbox is checked -
im bit confused how go problem. when user changes "quantity" dropdown, checks checkmark, when moved 0 checkmark unchecked.
var val = 0 $(.drop).on(change,function() { var val = $(this).val(); if(val > 0) { var product = $(this).attr("prodindex"); $(#switchname + product).prop(checked, true); } else { var product = $(this).attr("prodindex"); $(#switchname + product).prop(checked, false); } }); part of form:
<input class=check id=switchname0 type=checkbox> <label for=switchname0></label> </div> <div class=col-lg-8> <input id="order_products__product_id" name="order_products[][product_id]" type="hidden" value="4" /> test <br> <div class=subheader>$22.00</div> </div> <div class=col-lg-2> <select class="drop" data-cost-per-unit="2200" id="test" name="order_products[][quanity]" prodindex="0"><option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option></select> </div> </div> </div> </div> </div> i want create if checkmark not checked , check it, changes quantity 1 , if uncheck it, goes zero. since have other jquery here, if utilize $(.checkbox).on(change,fucntion(){} alter every time dropdown moved. whats improve solution problem?
the next code seems work, tests on fiddle. both checkbox , select impact each other. code assumes, code hints, checkboxes have id of form switchname#, # number.
dont forget alter 10 (switchname length) on actual code match ids.
var $drops = $(.drop); $(.check).on(change, function() { var val = $(this).prop(checked), product = this.id.slice(10); $drops.filter(function (i, e) { homecoming $(this).attr(prodindex) == product; }).val(val ? 1 : 0); }); $drops.on(change,function() { var val = +(this.value), product = $(this).attr("prodindex"); $(#switchname + product).prop(checked, val > 0); }); javascript jquery html
alternative link download
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.