我想知道如果从文本框中删除句号 .
是否有可能触发事件。
例如,我有一个文本框#Quantity
,它将数值作为带句号的输入
我有一个下拉控件#unitValue
,它有三个选项
我在句号 上成功命中了一个事件。
按键如下
$("#Quantity").keypress(function(e) {
if(e.which == 46) {
//successfully disabled Gram in Unit
}
});
现在,如果句号 .
从文本框中移除,我想在下拉菜单中启用“Gram”选项。
我有一个想法,但如果这样做是对的,就不要这样做。
想法是:-
在任意按键上将每个字母添加到数组中,然后在退格键按下时,我检查数组索引上的字母是否为句号 .
是否为.
签名然后启用“Gram”
任何帮助将不胜感激
谢谢
最佳答案
JS Fiddle - updated -使用 .prop()
相反
// attach the functionality on multi events
$('#inpt').on('keypress input change', function() {
var Gram = $('#slct').children('[value="1"]');
// if the indexOf "." is > -1, then there's a dot
if ($(this).val().indexOf('.') > -1) {
// disable the Gram option
Gram.prop('disabled', true);
// ====================== this part was added by @SarathChandra
// reset the drop down if Gram option was selected
if ($('#slct').val() === "1" || $('#slct').val() === null) {
$('#slct').val("0");
}
// ====================== @SarathChandra Thank you for the improvement
} else {
// if it the dot was removed, we set the disabled prop to false
Gram.prop('disabled', false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input id="inpt" type="text">
<select id="slct">
<option value="0">- select -</option>
<option value="1">Gram</option>
<option value="2">Kilo</option>
<option value="3">Quntal</option>
</select>
编辑感谢@SarathChandra添加了这几行:
if($('#slct').val() == "1" || $('#slct').val() == null){
$('#slct').val("0");
}
要在输入的值不包含 .
并选择 Gram 时修复此问题,THEN 添加了句点,它会重置下拉菜单。
关于javascript - 如何检查句号 (.) 是否从 jQuery 文本框中的单词中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34917316/