简体   繁体   中英

Run Function on every change in an input element: JQuery

I have a form called "wizard". Everytime any input element is changed, I want a Javascript function to run and calculate something. I have the following:

// update hidden field "channels" in wizard based on servers/cores
$('form#wizard select[name=servers], form#wizard select[name=cores]').change(function() {
 var channels = parseInt($('form#wizard select[name=servers]').val()) * parseInt($('form#wizard select[name=cores]').val());
 $('#yellow').val(channels);
 $('#yellow').change();
 alert(channels);
});

The above is wrapped around:

$(document).ready(function() {

The above does not seem to do what I want. Do I have to specifically have to have each input field with an onchnage action??

Thanks all

EDIT

Sorry I meant select elements.

<form id="wizard">
Number of server(s) you are thinking of purchasing licenses for:
<select style="margin-left: 10px; font-size:15px; padding:1px;" name="servers" id="servers"><option value="1" selected="">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</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><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option></select>
server(s) <br/><br/> Each server will have: <select  style="font-size:15px; padding:1px;" name="cores" id="cores"><option style="margin-left: 10px;" value="115">a single core CPU</option><option value="230" selected="">a dual core CPU</option><option value="500">a quad core CPU</option><option value="960">an octo core CPU</option></select><br/><br/> And will be running: <select style="margin-left: 10px; font-size:15px; padding:1px; margin-right: 10px;" name="app" id="app"><option value="Asterisk" selected="">Asterisk®</option><option value="FreeSWITCH">FreeSWITCH</option></select> On
<select style="margin-left: 10px; font-size:15px; padding:1px;" name="cores" id="cores"><option value="Linux">Linux</option><option value="Solaris 10">Solaris 10</option><option value="OpenSolaris">OpenSolaris</option></select>
<br /><br />
</form>

EDIT2

Is it a problem if this form is in a div that is hidden? I am grabbing at straws here, i have no idea how to fix this!

When I remove from display:none, it for some reason works! Btw, I am using this with fancybox jquery plugin.

http://fancybox.net/js/fancybox/jquery.fancybox-1.2.1.js

What is "#yellow"?

$('form#wizard select').change(function() {
  var channels = (parseInt($('form#wizard select[name=servers]').val(), 10) *
    parseInt($('form#wizard select[name=cores]:first').val(), 10));
  $('#yellow').val(channels);
  $('#yellow').change();
  alert(channels);
});

You have two select's with a name and ID of cores, that may be the problem. I added the 10 to parseInt as generally that is what people want and parseInt can do strange things without it. Does "#yellow" also have a change event attached to it? if not you dont need to trigger the change, even then, setting its value should evoke the change event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM