javascript小写onchange

javascript小写onchange,javascript,onchange,lowercase,keyup,Javascript,Onchange,Lowercase,Keyup,我希望你能帮我解决一个小问题。我哪儿也去不了 如果在字段post_标题中输入,则应将字符自动复制到字段post_uri中。这很有效 现在,所有字母都应该自动小写,空格应该替换为-字符 我该怎么办 $(函数(){ var$post_title=$(“#post_title”); var$post_uri=$('post_uri'); 函数onChange(){ $post_uri.val($post_title.val()); }; $(“#文章标题”) .change(onChange) .k

我希望你能帮我解决一个小问题。我哪儿也去不了

如果在字段post_标题中输入,则应将字符自动复制到字段post_uri中。这很有效

现在,所有字母都应该自动小写,空格应该替换为-字符

我该怎么办

$(函数(){
var$post_title=$(“#post_title”);
var$post_uri=$('post_uri');
函数onChange(){
$post_uri.val($post_title.val());
};
$(“#文章标题”)
.change(onChange)
.keyup(onChange);
}); 

你就快拿到了。只需在字符串上添加
toLowerCase
replace
函数调用

$(函数(){
var$post_title=$(“#post_title”);
var$post_uri=$('post_uri');
函数onChange(){
var val=$post_title.val().toLowerCase().replace(/\s/g,“-”);
$post_uri.val(val);
};
$(“#文章标题”)
.change(onChange)
.keyup(onChange);
});

变量
假设这是你的文章标题

如何安装Windows 10

我认为您正在尝试将url设置为类似于
example.com/how-to-setup-windows-10

代码

试试这个代码,我想它会有用的


调用toLowerCase()??是的,我知道。。。var$post_title=post_title.toLowerCase();不起作用var$post\u title=post\u title.replace(“”,/-/g).toLowerCase()只是一个附加注释。如果通过鼠标右键单击粘贴文本,则此解决方案不起作用。如果通过
CTRL-V
var space_url = $("#post_title").val().replace(" ", "-");
$("#post_url").val(space_url.toLowerCase());