javascript小写onchange
javascript lowercase onchange
我希望你能帮我解决一个小问题。我哪儿也去不了。
如果在字段 post_title 中进行输入,字符应自动复制到字段 post_uri 中。行得通。
现在所有字母都应自动小写,空格应替换为 - 字符。
我需要做什么?
$(function() {
var $post_title = $('#post_title');
var $post_uri = $('#post_uri');
function onChange() {
$post_uri.val($post_title.val());
};
$('#post_title')
.change(onChange)
.keyup(onChange);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<form>
<input type="text" name="post_title" id="post_title">
<input type="text" name="post_uri" id="post_uri">
</form>
你差不多明白了。您只需要在字符串上添加 toLowerCase
和 replace
函数调用。
$(function() {
var $post_title = $('#post_title');
var $post_uri = $('#post_uri');
function onChange() {
var val = $post_title.val().toLowerCase().replace(/\s/g, "-");
$post_uri.val(val);
};
$('#post_title')
.change(onChange)
.keyup(onChange);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<form>
<input type="text" name="post_title" id="post_title">
<input type="text" name="post_uri" id="post_uri">
</form>
编辑: 澄清一下,此解决方案在任何内容被修改的情况下都有效。
.change()
For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.
变量
假设这是你的post标题
How to Setup Windows 10
我认为你正在努力让你的 url 像 example.com/how-to-setup-windows-10
。
代码
Try this code I think it will work.
var space_url = $("#post_title").val().replace(" ", "-");
$("#post_url").val(space_url.toLowerCase());
我希望你能帮我解决一个小问题。我哪儿也去不了。
如果在字段 post_title 中进行输入,字符应自动复制到字段 post_uri 中。行得通。
现在所有字母都应自动小写,空格应替换为 - 字符。
我需要做什么?
$(function() {
var $post_title = $('#post_title');
var $post_uri = $('#post_uri');
function onChange() {
$post_uri.val($post_title.val());
};
$('#post_title')
.change(onChange)
.keyup(onChange);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<form>
<input type="text" name="post_title" id="post_title">
<input type="text" name="post_uri" id="post_uri">
</form>
你差不多明白了。您只需要在字符串上添加 toLowerCase
和 replace
函数调用。
$(function() {
var $post_title = $('#post_title');
var $post_uri = $('#post_uri');
function onChange() {
var val = $post_title.val().toLowerCase().replace(/\s/g, "-");
$post_uri.val(val);
};
$('#post_title')
.change(onChange)
.keyup(onChange);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<form>
<input type="text" name="post_title" id="post_title">
<input type="text" name="post_uri" id="post_uri">
</form>
编辑: 澄清一下,此解决方案在任何内容被修改的情况下都有效。
.change()
For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.
变量
假设这是你的post标题
How to Setup Windows 10
我认为你正在努力让你的 url 像 example.com/how-to-setup-windows-10
。
代码
Try this code I think it will work.
var space_url = $("#post_title").val().replace(" ", "-");
$("#post_url").val(space_url.toLowerCase());