在 Jquery 中替换 & 小写

Replace & lowercase in Jquery

我有一个文本框,在输入文本时需要将其复制到其他文本框中,并且单词之间的间距需要用破折号替换,并且应该将其转换为小写。

我有以下代码,但它不起作用

$(document).ready(function() {
$('#Name').keyup(function(e) {
    var txtVal = $(this).val();
    txtVal = txtVal.toLowerCase();
    $('#URL').val(txtVal);
});

});

需要帮助,可以做什么??

替换为小写后,将所有[space]替换为-

$(document).ready(function() {
  $('#Name').keyup(function(e) {
    var txtVal = $(this).val();
    txtVal = txtVal.toLowerCase().replace(/\s/g, '-');
    $('#URL').val(txtVal);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="Name" />
<input id="URL" />

将所有空白字符替换为-

$(document).ready(function() {
  $('#Name').keyup(function(e) {
    var txtVal = $(this).val();
    txtVal = txtVal.toLowerCase().replace(/\s/g, '-');
    $('#URL').val(txtVal);
  });
});

您也可以按照以下步骤操作:

  1. toLowerCase()
  2. split(' ')
  3. join('-')

代码片段:

txtVal = txtVal.toLowerCase().split(' ').join('-');

Fiddle Example