美好的一天。 我使用此代码将值从一个输入复制到另一个输入:
<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);
谢谢。
答案 0 :(得分:1)
简单的解决方案,只需将tmp_str
替换为which
,如下所示:
<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>