如何使用jQuery将一些文本更改为小写并用连字符替换空格? [英] How can I change some text to lowercase and replace spaces with hyphens using jQuery?

查看:94
本文介绍了如何使用jQuery将一些文本更改为小写并用连字符替换空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个隐藏的输入字段,它将获取另一个keyup的值,我正在试图找出如何将隐藏字段中的值转换为小写,并用连字符替换空格。

I have a hidden input field that will take the value of another on keyup and I'm trying to figure out how transform the value in the hidden field to lowercase and replace spaces with hyphens.

所以,如果有人在标题输入栏输入This is A Sample,则标识符输入栏将被设置为this-is-a-sample。

So, if someone types "This Is A Sample" in the title input field, the identifier input field will be set to "this-is-a-sample".

<input type="text" name="title" value="This Is A Sample" />
<input type="hidden" name="identifier" value="this-is-a-sample />


推荐答案

这将用 -

<script type="text/javascript">
$(document).ready(function(){
var test= $('input[name="title"]').val();
test = test.toLowerCase().replace(/ /g, '-');
$('input[name="identifier"]').val(test);
}):
</script>

这篇关于如何使用jQuery将一些文本更改为小写并用连字符替换空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆