【问题标题】:Input, onChange CopyIt输入,onChange CopyIt
【发布时间】:2015-05-08 08:28:34
【问题描述】:

我有这个:

它将我在 input1 中输入的内容复制到 input2 并去除空格等。

但它只会在我将鼠标悬停在输入 2 上时复制,是否有可能在输入时复制?

输入1

<input name="input1" type="text" id="input1" size="60" onchange = "copyItt()" />

输入2

<input onclick="javascript:stripspaces(this)" onmouseover="javascript:stripspaces(this)" onkeydown="javascript:stripspaces(this)" name="input2" type="text" id="input2" readonly onmousemove="javascript:this.value=this.value.toLowerCase();" onblur="javascript:this.value=this.value.toLowerCase();"  size="60" />

javascript

function copyItt() {
var x = document.getElementById("input1").value;
document.getElementById("input2").value = x;
}

function stripspaces(input) 
{
input.value = input.value.replace(/\s/gi,"-");

【问题讨论】:

  • 仅供参考,您不需要在 onclick 等 javascript 事件处理程序中使用 javascript: 前缀。仅当您在 href 属性中使用 javascript 时才需要该前缀(它充当协议,让浏览器知道将后面的内容作为 javascript 执行,而不是将其作为 URL 处理)。

标签: javascript input onchange


【解决方案1】:

您似乎想将onkeyup="copyItt()" 添加到intput1,并且您可能还想从copyItt() 内部调用stripspaces(),以便在您键入时处理文本:

function coypItt() {
    document.getElementById("input1").value;
    document.getElementById("input2").value = x;
    stripspaces(document.getElementById("input1"));
}

有了这个,您可能会发现input2 上的许多(全部?)事件处理程序是不必要的。

【讨论】:

    猜你喜欢
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多