简体   繁体   English

如果文本框中的值大于1,则更改为复数

[英]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/ 这是一个JSFiddle: http : //jsfiddle.net/T3Sxb/

As you can see, the select options are in the form of word(s) 如您所见,选择选项为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 如果输入框中的数字为1,则选项中的值将为word形式
  • If the number in the inputbox is greater than 1, the value in the options are plural. 如果输入框中的数字大于1,则选项中的值为复数。

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. 在许多情况下,仅添加“ s”并不起作用。

Also note that zero items usually (always?) takes a plural form. 还要注意,零项通常(总是?)采用复数形式。

HTML 的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 的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/ 您可以尝试以下方法: 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. 从用户体验的角度来看,我认为(s)是完全可以接受的。 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')));
        }
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Javascript 中制作复数值(大于、小于) - Making a value plural (Greater than, Less than) in Javascript jQuery如果value大于1,如何更改CSS - jQuery How to change CSS if value is greater than 1 AngularJs输入文本框仅允许大于零的值 - AngularJs Input textbox allow only greater than zero value 如何检查一个文本框的值是否应小于另一个文本框的值,以及两个值的总和应不大于1 - How to check if one textbox value should be less than another textbox value and also the sum of both values should not be greater than 1 对于大于 1 的月份值,EST 时区中的时区变化了 1 小时 - Timezone is getting change by 1 hour in EST timeZone for month value greater than 1 如果它大于或等于javascript中的某物,则想更改某物的值 - Want to change the value of something if its greater than or equal to something in javascript 如何检查文本框是否为非空并且其值大于0而不是文本? - How to check if textbox is non-empty and that its value is greater than 0 and not text? 如何检查文本框中的一个值是否大于另一个值,如果是,则显示跨度错误 - How to check if one value in textbox is greater than the other, if so show error in span 检查文本框的数量是否大于输入的文本框数组的数量 - Check if the number of a textbox is greater than number entered array of textbox's 当值大于1时,执行此操作 - When value is greater than 1, do this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM