当用户使用javascript键入时,我想将小写字母转换为大写.欢迎任何建议.
我尝试过以下方法:
$("#textbox").live('keypress', function (e) {
if (e.which >= 97 && e.which <= 122) {
var newKey = e.which - 32;
// I have tried setting those
e.keyCode = newKey;
e.charCode = newKey;
}
});
Run Code Online (Sandbox Code Playgroud) 我正在使用正则表达式从javascript中的文本输入区域中删除无效字符(在IE中运行).我在每个keyup事件上运行replace函数.但是,这会使光标在每次按键后跳转到文本框的末尾,这使得无法进行内联编辑.
这是它的实际应用:
有谁知道如何使它,所以光标不会跳转到输入框的末尾?
我想在输入中的每第4个字符后插入一个破折号.我有一个信用卡输入框.当用户输入并到达每个第4个字符时,jQuery将插入连字符(-).
例如: 1234-5678-1234-1231
更新:我正在尝试一些代码,我认为我是如此接近正确的代码,但我有一些问题.这是我的代码示例;
$('.creditCardText').keyup(function() {
var cardValue = $('.creditCardText').val(),
cardLength = cardValue.length;
if ( cardLength < 5 ) {
if ( cardLength % 4 == 0 ) {
console.log('4 lük geldi');
cardValue += "-";
$('.creditCardText').val(cardValue);
}
} else {
if ( cardLength % 5 == 0 ) {
console.log('5 lük geldi');
cardValue += "-";
$('.creditCardText').val(cardValue);
}
}
});
Run Code Online (Sandbox Code Playgroud) 在使用C#的asp.net-mvc项目中.
我使用一个函数用逗号来格式化更大的数字,例如1,000,000,感谢这篇文章:
function numberWithCommas(str) {
return str.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
Run Code Online (Sandbox Code Playgroud)
问题是,我将输入锁定为仅接受最小值为零的数字.
<input type="number" min="0" class="myclass" value="@somevalue" />
Run Code Online (Sandbox Code Playgroud)
这会引起使用JS的问题,因为它只需要数字输入.这让我想到这样一个问题如何让HTML输入标签只接受数值?,它还提供了JS解决方案.
我想知道是否有人开发了一种优雅的方式来格式化数字输入显示,同时验证数字输入,这里还有其他选项吗?它不一定纯粹是JS解决方案.
我的反应组件上有一个输入字段,显示项目的行价(两个小数位,有千位分隔符).我希望当组件首次呈现时显示的值为货币格式,并且还要作为字段中的用户类型以货币格式保存.
目前我的组件中有以下代码:
var React = require('react');
import accounting from 'accounting';
MoneyInput = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired,
onChange: React.PropTypes.func.isRequired,
value: React.PropTypes.number,
error: React.PropTypes.string,
},
onChange(event) {
// get rid of any money formatting
event.target.value = accounting.unformat(event.target.value);
// pass the value on
this.props.onChange(event);
},
getValue() {
return accounting.formatNumber(this.props.value, 2)
},
render() {
return (
<div className="field">
<input type="text"
name={this.props.name}
className="form-control"
value={this.getValue()}
onChange={this.onChange} />
<div className="input">{this.props.error}</div>
</div>
);
}
});
module.exports = MoneyInput;
Run Code Online (Sandbox Code Playgroud)
该代码显示正确格式化的数据,但每次输入值时,光标都会跳转到数字的末尾.
我理解为什么会发生这种情况(我认为),并且我在这里阅读了几个与不在JavaScript中丢失光标位置有关的问题(例如这里和这里).
我想知道在React中处理这个问题的最佳方法是什么?我认为理想情况下我不希望将光标位置存储在状态中(例如,我希望这些是Dan Abramov语法中的Presentation …
此正则表达式将每个小写字词转换为大写字母.我有一个全名输入字段.我希望用户看到他/她按下的每个单词的第一个字母在输入字段中转换为大写.
我不知道如何正确替换当前输入字段中的选定字符.
$('input').on('keypress', function(event) {
var $this = $(this),
val = $this.val(),
regex = /\b[a-z]/g;
val = val.toLowerCase().replace(regex, function(letter) {
return letter.toUpperCase();
});
// I want this value to be in the input field.
console.log(val);
});
Run Code Online (Sandbox Code Playgroud) 我需要具有TextArea功能的功能
1) maximum total lines- 6 and
2) in each line there must be maximum of 16 chars
3) if user enters 17th character the cursor should go to the next line
and user will type in there (the line will be counted)
4) if user reaches to the 7th line it will not allow user to write
5) if user type e.g "Hello, I Love StackOverflow and its features" (counting
from 1st Char 'H', the 16th char is 't' …Run Code Online (Sandbox Code Playgroud) 我使用toLowerCase()将内容可编辑div转换为小写.但它也扭转了文本!有人可以告诉我为什么会这样吗?
$(document).ready(function() {
$('#user').bind('keyup', function() {
$(this).text($(this).text().toLowerCase());
});
});Run Code Online (Sandbox Code Playgroud)
#user {
background: #f1f1f1;
padding: 1em;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="user" contenteditable="true"></div>Run Code Online (Sandbox Code Playgroud)
小提琴: https ://jsfiddle.net/professorsamoff/8zyvc202/3/
蒂姆,谢谢
javascript ×7
jquery ×5
asp.net-mvc ×1
c# ×1
formatting ×1
html ×1
keycode ×1
lowercase ×1
react-jsx ×1
reactjs ×1
regex ×1
replace ×1
textarea ×1
uppercase ×1
validation ×1