It is impossible to use if properly, or the error is completely different.

The script considers the amount of services ordered:

  1. The day and level is #days from <select> 's #days and #lvl . The amount is written to the variable up .

  2. On the calendar, select the numbers for advertising, are recorded in the <input> #altField . It also calculates the number of days that is written to the reklama variable.

So I put in all the code to see how it works. http://jsfiddle.net/8o28p0hy choose level 1 and days 2 for example. should calculate 0.6, but counts 1.2, but when the value appears in the advertising, even when clicked on the input, the amount getting right - 0.6

for clarity, input advertising considers comma-separated values ​​with a space.

  • show dayCount code. just in case - it most likely returns a string, not a number. and the line == true - PashaPash
  • function dayCount (val) {try {return {days: val.match (/ \ S + / g) .length}} catch (ex) {return {days: 0}; }} - akasergej
  • so what exactly is the problem? you return a non-zero object c. if (c) is always true. what exactly is not working? - PashaPash
  • that's the problem that I get with always work and then even if the days and lvl are not filled, I don’t care what they think ... - akasergej
  • one
    because if doesn't know what to check c.days. he check that c! = null. if you need to check c.days - just write - if (c.days) - PashaPash

1 answer 1

The dayCount function dayCount returns a non-zero object — either the number of days or the stub { days: 0 } .

if treats a non-zero object as true. If you want to check for a non-zero number of days, replace with

 if (c.days) { 

or at

 if (c.days > 0) { 
  • nothing has changed ... - akasergej
  • so I put in all the code to see how jsfiddle.net/8o28p0hy works, for example, select level 1 and days 2. should calculate 0.6, and counts 1.2, but when the value appears in the input advertisement, even when clicked on the input, the amount already becomes correct - 0.6 - akasergej
  • @akasergej you write code at random? in the handler this is the element on which the event occurred. you at the very beginning of the handler take the value of this.value . when you select a value from the dropdown - this is the dropdown. and comes 1 day. when you click on the input - this is an input. this.value - already the value of the input is empty - 0 days. Naturally, you get a different result. - PashaPash
  • Yes, in general, then at random. blinded two working codes into one separately, and since javascripte doesn’t make such a mess. Could you fix it right? - akasergej
  • one
    @akasergej replace this.value with $("#idтогоэлементаизкоторогонужновзятьзначение").val() . which one - you better know) - PashaPash