0

再会。我使用此代码将值从一个输入复制到另一个:

<script type = "text/javascript">
function transfer(which) {
document.getElementById("temp_name").value = which;
}
</script>
<form action="register.php" method="post" name="register">
<input type="text" name="username" id = "username" onkeyup = "transfer(this.value)"><br>
<input type="text" name="temp_name" id = "temp_name">
</form>

这个重复的#username 到#temp_name 但我需要添加的文本#temp_name 没有额外的连字符和小写。所以我需要以某种方式将此代码添加到上层代码中:

.replace(/\s/g,'-'); // to replace spaces with hypens
tmp_str = tmp_str.replace(/[\-]+/g,'-');    // to remove extra hypens
tmp_str = tmp_str.replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase(); // to convert to lower case and only allow alpabets and number and hypehn
tmp_str = alltrimhyphen(tmp_str);

谢谢你。

4

1 回答 1

1

简单的解决方案,只需像这样替换tmp_strwhich

<script type="text/javascript">
    function transfer(which) {
        which = which.replace(/\s/g,'-'); // to replace spaces with hypens
        which = which.replace(/[\-]+/g,'-');    // to remove extra hypens
        which = which.replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase(); // to convert to lower case 
        document.getElementById("temp_name").value = which;
   }
</script>

<form action="register.php" method="post" name="register">
    <input type="text" name="username" id="username" onkeyup="javascript:transfer(this.value);">
    <br />
    <input type="text" name="temp_name" id="temp_name">
</form>
于 2012-09-17T14:58:48.777 回答