简体   繁体   English

在选择框中选择其他选项时更改数字值

[英]Change number value on selection of different option in selectbox

Simply want to change values of my input type number on the selection of a different option from the select box. 只是想从选择框中选择其他选项来更改我的输入类型编号的值。

I can apply if else then do this but I want a less complex and few lines code which works easily. 如果可以的话,我可以申请,但我想要一个更简单,行数少的代码,它很容易工作。 For eg: If I choose A, B, C then the value of input will be "A" same if I choose a,b,c then the value of input will be "a", Basically I want their initials only in input type number. 例如:如果我选择A,B,C,则输入的值将为“ A”;如果我选择a,b,c,则输入的值将为“ a”。基本上,我只希望它们的缩写以输入类型数。

HTML: HTML:

<select name="number_select" id="number_select" class="number_select div_height font_size" accesskey="n">
<option value="1" selected="selected">1, 2, 3, ...</option>
<option value="2">a, b, c, ...</option>
<option value="3">A, B, C, ...</option>
<option value="4">i, ii, iii, ...</option>
<option value="4">I, II, III, ...</option>
</select>
<input type="number" name="start_number" id="start_number" class="start_number div_border font_size" value="1" min="1" max="9" accesskey="s"/>

You can run the code snippet and check the values in console.log but the values won't reflect in the input since you have defined its type as number 您可以运行代码片段并检查console.log的值,但是由于您将其类型定义为number ,因此这些值不会反映在input

 $("#number_select").change(function() { var selectedValue = $(this).find("option:selected").text(); console.log("Initials: " + selectedValue[0]); $("#start_number").val(selectedValue[0]); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="number_select" id="number_select" class="number_select div_height font_size" accesskey="n"> <option value="1" selected="selected">1, 2, 3, ...</option> <option value="2">a, b, c, ...</option> <option value="3">A, B, C, ...</option> <option value="4">i, ii, iii, ...</option> <option value="4">I, II, III, ...</option> </select> <input type="number" name="start_number" id="start_number" class="start_number div_border font_size" value="1" min="1" max="9" accesskey="s" /> 

 $("#number_select").change(function() { var option = $(this).find("option:selected").text(); if ($.isNumeric( option[0] )) $("#start_number").attr("type", "number"); else $("#start_number").attr("type", "text"); $("#start_number").val(option[0]); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="number_select" id="number_select" class="number_select div_height font_size" accesskey="n"> <option value="1" selected="selected">1, 2, 3, ...</option> <option value="2">a, b, c, ...</option> <option value="3">A, B, C, ...</option> <option value="4">i, ii, iii, ...</option> <option value="4">I, II, III, ...</option> </select> <input type="number" name="start_number" id="start_number" class="start_number div_border font_size" value="1" min="1" max="9" accesskey="s" /> 

Try this: 尝试这个:

$('#number_select').change(() => {
    $('#start_number').val($('#number_select').val());
});

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

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