从MS Excel复制粘贴在IE中失败,但在Firefox中有效



我用一个PHP表单和一点Javascript构建了一个非常简单的应用程序。

在我的表单中,我有一个文本输入,用于运行数据库搜索。如果我有多个值,我会有一些代码在每个值之间加一个逗号。

奇怪的是:

在Firefox中,我可以执行MS Excel,复制5个值并将它们粘贴到文本输入控件中。我可以看到粘贴的所有5个值以及中间的逗号。

在Internet Explorer版本8中,我可以使用MS Excel,复制5个值,但只有一个值(第一个数字)粘贴在文本输入控件中。

这是我的html

<fieldset>
<label for="DBRIDs">RIDs</label><input type="text" id="DBRIDs" name="DBRIDs" onchange = "removespaces(this)">
</fieldset>

这是我的页头中的Javascript

<script language="javascript" type="text/javascript">
function removespaces(which) {
str = which.value;
str = str.replace (/s|n/g,",");  // replace space or newline by commas
document.myform.DBRIDs.value = str;
}

非常基本的东西。我错过了什么?为什么IE不能像Firefox一样粘贴??

编辑

我有一个打字错误,所以现在可以使用文本区域了。我可以复制一个列并从IE 粘贴它

当然(讽刺),它引入了一个新问题:它重复了我的逗号,我不清楚这是因为textarea或我的Javascript。

具有文本类型的输入不支持换行符。因此,IE会自动剥离它们,而且你永远没有机会解析它们。你最好的选择可能是使用文本区域。

或者,您可以执行以下操作,但这实际上只是一个变通方法(请注意onpaste事件):

<fieldset>
<label for="DBRIDs">RIDs</label><input type="text" id="DBRIDs" name="DBRIDs" onchange="removespaces(this)" onpaste="handlePaste(this)">
</fieldset>

对于javascript,类似于:

function removespaces (which) {
    var str = which.value;
    str = str.replace(/s|n/g, ","); // replace space or newline by commas
    which.value = str;
}
function handlePaste (which) {  
    var str = window.clipboardData.getData("Text");    
    str = str.replace(/s|n/g, ","); // replace space or newline by commas
    which.value = str;
    return false; // kill the paste event so you don't get duplicate data.
}

jsfiddle示例如下:http://jsfiddle.net/fordlover49/mJ7L3/

在一个相关的注意事项上(为了尽量帮助您避免进一步的问题),您将元素传递给removespaces函数,但对您正在设置的元素进行硬编码,而不是使用传递的元素。

相关内容

最新更新