Tuesday 15 April 2014

javascript - Change to plural if value in textbox is greater than 1 -


I have a text box and there is a selection box like this:

  & lt ; H3 & gt; Yield Yield & lt; / H3 & gt; & Lt; Input style = 'width: 100px' type = "text" name = "yield" square = "small" /> & Lt; Select Name = 'Overhead Type' & gt; & Lt; Option value = 'Servings' & gt; Served (s) & lt; / Option & gt; & Lt; Option value = 'cup' & gt; Cup (s) & lt; / Options & gt; & Lt; Option value = 'loops (receipt)' & gt; Loaves (loose) & lt; / Options & gt; & Lt; / Select & gt; Here is a JSFiddle:  

As you can see, the selected option as word (word)

But I have a script where

  • is the number of the input box 1, the value in the value word
  • if the number in the input box If there is more than 1, then the values ​​in the options are plural.

    Is this possible? how can I do this? Thanks for all the help!

  • Optional input [type = " Multiple input, style:

    I am using data attributes so that you can declare the appropriate singular for each item. Just adding "S" to plural form does not work in many cases.

    Also keep in mind that normally (always?) Zero things take plural form.

    HTML

      & lt; Input style = 'width: 100px' type = "text" id = "yield" square = "small" /> & Lt; Select ID = 'yield type' & gt; & Lt; Option value = 'Surating' data-Single = "Service" data-other = "Surtaining" & gt; & Lt; / Option & gt; & Lt; Option value = 'cup' data-single = "cup" data-other = "cup" & gt; & Lt; / Options & gt; & Lt; Option value = 'loaves (receipt)' data-singles = "loose" data-other = "lowe" & gt; & Lt; / Options & gt; & Lt; / Select & gt;   

    Javascript

      var yield = $ ("# yield"); Var yield type = $ ("#product type"); Function Evaluation () {var single = parseInt (yield.val (), 10) === 1; $ ("Option", sub-type) .each (function () {var option = $ (this); if (single) {option.text (option.attr ("data-single"))} and {option .text (Option.attr ("Data-Other"));}}); } // Whatever you want to trigger the change, it should yield here On ("keyboard", evaluation); // Evaluation Evaluation ();    

No comments:

Post a Comment