简体   繁体   中英

Change to plural if value in textbox is greater than 1

I have a textbox and a selectbox like this:

<h3>Recipe Yield</h3>
<input style='width:100px' type="text" name="yield" class="small" />
<select name='yieldType'>
    <option value='Servings'>Serving(s)</option>
    <option value='Cups'>Cup(s)</option>
    <option value='Loaves (Loaf)'>Loaves (Loaf)</option>
</select>

Here's a JSFiddle: http://jsfiddle.net/T3Sxb/

As you can see, the select options are in the form of word(s)

but I want to have a script where when

  • If the number in the inputbox is 1, the value in the options are in the form of word
  • If the number in the inputbox is greater than 1, the value in the options are plural.

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

I'm using data attributes so that you can declare the proper singular/plural forms for each item. Simply adding "s" doesn't work in many cases.

Also note that zero items usually (always?) takes a plural form.

HTML

<input style='width:100px' type="text" id="yield" class="small" />
<select id='yieldType'>
    <option value='Servings' data-single="Serving" data-other="Servings"></option>
    <option value='Cups' data-single="Cup" data-other="Cups"></option>
    <option value='Loaves (Loaf)' data-single="Loaf" data-other="Loaves"></option>
</select>

JavaScript

var yield = $("#yield");
var yieldType = $("#yieldType");

function evaluate(){
    var single = parseInt(yield.val(), 10) === 1;
    $("option", yieldType ).each(function(){
        var option = $(this);
        if(single){
            option.text(option.attr("data-single"));
        }else{
            option.text(option.attr("data-other"));
        }
    });
}

// whatever events you want to trigger the change should go here
yield.on("keyup", evaluate);

// evaluate onload
evaluate();

You could try this: http://jsfiddle.net/T3Sxb/7/

var plural = {
    Serving: "Servings",
    Cup: "Cups",
    Loaf: "Loaves"
};

var singular = {
    Servings: "Serving",
    Cups: "Cup",
    Loaves: "Loaf"
};

$( "#pluralizer" ).on( "keyup keydown change", function() {
    var obj = parseInt( $( this ).val() ) === 1 ? singular : plural;
    $( "#YieldType option" ).each( function() {
        var html = $( this ).html();
        if ( html in obj ) {
            $( this ).html( obj[html] );
        }
    });
});

From a UX point of view, I think (s) is perfectly acceptable. But anyway, how about this:

<option value='Servings' data-singular="Serving" data-plural="Servings">Servings</option>

then:

// you should really use IDs ;)
$('input[name="yield"]').on('change', function () {
    var singular = parseInt($(this).val(), 10) === 1;
    $('select[name="yieldType"]').each(function () {
        if (singular) {
            $(this).val($(this.attr('data-singular')));
        } else {
            $(this).val($(this.attr('data-plural')));
        }
    });
});

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