jquery - Add/Remove values to field from checkbox values in Javascript -


i have 2 fields: 1 checkbox (built scala), 1 input/text field. trying add , remove values checkbox input field. trying take multiple values , string comma.

here html fields:

<div class="column column1">     @for(service <- serviceslist) {         <label><input type="checkbox" name="selectservices" value=@service.name><span>@service.name</span></label>     } </div>  <input name="services" id="services"> 

i using jquery in tag try record onchange event:

$(document).ready(function(){      var $services = $('#services');     var $selectservices = $('#selectservices');      $selectservices.change(function(){         (var = 0, n = this.length; < n; i++) {             if (this[i].checked) {                 $services.val($services.val() + this[i].value);             }             else {                 $services.val($services.val().replace(this[i].value, ""));              }         }      });  }); 

however, seems not "fire" when checking , unchecking checkbox. not receive errors or messages, guessing not working or code incorrect.

i appreciate help!

try example, don't have search , replace time, set new value:

$(function() {    $('input[name=selectservices]').on('change', function() {      $('#services').val($('input[name=selectservices]:checked').map(function() {        return this.value;      }).get());    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="column column1">    <label>      <input type="checkbox" name="selectservices" value='1'><span>1</span>    </label>    <label>      <input type="checkbox" name="selectservices" value='2'><span>2</span>    </label>    <label>      <input type="checkbox" name="selectservices" value='3'><span>3</span>    </label>    <label>      <input type="checkbox" name="selectservices" value='4'><span>4</span>    </label>  </div>  <input name="services" id="services">

does $(function() {} go $(document).ready(function(){}?

no, short-hand or equivalent same.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -