如何使用jQuery将某些文本更改为小写并用连字符替换空格?

Kar*_*rma 29 jquery input dynamic

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

因此,如果有人在标题输入字段中键入"This Is A Sample",则标识符输入字段将设置为"this-is-a-sample".

<input type="text" name="title" value="This Is A Sample" />
<input type="hidden" name="identifier" value="this-is-a-sample />
Run Code Online (Sandbox Code Playgroud)

Alp*_*esh 56

这将取代所有空间 -

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


小智 15

要转换为小写:

var lowercase = 'This Is A Sample'.toLowerCase();
Run Code Online (Sandbox Code Playgroud)

要替换空白区域:

var Replace= 'This Is A Sample'.replace(/ /g,"-");
Run Code Online (Sandbox Code Playgroud)

看看这个例子

@JSbin