简体   繁体   English

使用Javascript更改输入文本值不会更改显示

[英]Changing input text value with Javascript doesn't change display

I'm trying to change the value of a text input field based on user actions. 我正在尝试根据用户操作更改文本input字段的值。 I'm doing it like this: 我是这样做的:

document.getElementById(textFieldID).value = newValue;

It isn't quite working -- the original text in the field remains on the screen, unchanged. 它不是很有效 - 该字段中的原始文本仍保留在屏幕上,不变。 However, when I submit the form, it behaves as though the value was indeed changed correctly. 但是,当我提交表单时,它的行为似乎确实正确地更改了值。 (And a debug alert confirms that yup, I'm hitting that bit of the code and passing in the right field ID and text value.) Anybody have any insights? (并且调试alert确认是的,我正在点击代码并传入正确的字段ID和文本值。)任何人都有任何见解? Is there something I need to be doing to redraw the input element? 我需要做些什么才能重绘input元素?


Edit: Per Jeff B's request, and per the fact that this seems to have everybody stumped, here's some relevant bits of code: 编辑:按照Jeff B的要求,并且根据这似乎让每个人都难过,这里有一些相关的代码:

 <script LANGUAGE="JavaScript" TYPE="text/javascript"> function changeText(changeSelector) { var myindex = document.getElementById(changeSelector+"Recent").selectedIndex; var SelValue = document.getElementById(changeSelector+"Recent").options[myindex].value; document.getElementById(changeSelector).value = SelValue; document.getElementById("historicalText").value = SelValue; document.getElementById("historicalTextSelect").value = changeSelector; } </script> <input onChange="updateScrollingPreview1217(this); return true;" type="text" id="crawlMsg1217" name="crawlMsg1217" size="60" maxlength="1000" value=""> <select id="crawlMsg1217Recent" name="crawlMsg1217Recent" onchange="javascript:changeText('crawlMsg1217');"> [options go here] </select> 

And that "onChange" handler isn't what's gumming up the works; 而那个“onChange”处理程序并不是正在搞砸的工作; I get the same behavior with or without it. 无论有没有,我都会得到相同的行为。


Edit 2: It looks like the problem is being caused by "JSpell", a third-party spelling checker our product uses. 编辑2:看起来问题是由“JSpell”引起的,这是我们产品使用的第三方拼写检查程序。 (I'm told that clients prefer using it to a spellcheck built into the browser; go figure.) It appears to be slightly misconfigured on my test machine, so I'm going to try straightening that out and praying that it makes the problems go away. (我被告知客户更喜欢将它用于浏览器中内置的拼写检查;如图所示。)我的测试机上看起来有点配置不当,所以我会尝试将其整理出来并祈祷它会产生问题走开。 If it doesn't ... should be interesting. 如果不......应该很有趣。
Edit 3: Yup. 编辑3:是的 Fscking JSpell. Fscking JSpell。 Just posted a complete answer for the sake of posterity, will accept it tomorrow when I'm allowed. 刚刚为了后代发布了一个完整的答案,明天我会被允许接受它。 My thanks to everybody who tried to help; 感谢所有试图帮助的人; +1's all around, wish I could give more. + 1's四周,希望我能给予更多。

I have confirmed that the culprit is indeed JSpell, and that the precise trouble spot is this line: 我已经确认罪魁祸首确实是JSpell,而确切的故障点是这一行:

window.onload=jspellInit;

Despite the prayers mentioned in Edit 2 above, making sure it was configured correctly did NOT make the problem go away. 尽管上面的编辑2中提到了祈祷,但确保它配置正确并没有使问题消失。 And this line is indispensable to JSpell's functionality. 这条线对于JSpell的功能是不可或缺的。 I don't know if JSpell always hoses Javascript functionality this way, or if there's some sort of perfect storm of factors that's causing it to pick a fight with my page, but that is indeed the source of my problems. 我不知道JSpell是否总是以这种方式使用Javascript功能,或者是否存在一些完美的因素风暴导致它选择与我的页面进行斗争,但这确实是我的问题的根源。

My thanks to everybody who tried to help. 感谢所有试图提供帮助的人。 This was obviously a bit of a no-win in terms of getting the right answer, since it was caused by a component that was seemingly entirely unrelated and thus didn't get mentioned by me, but you at least confirmed that I was (in theory) doing things right and not simply going insane. 在获得正确答案方面,这显然有点不可取,因为它是由一个看起来完全不相关的组件引起的,因此我没有提到,但你至少证实我是(在做事正确而不是简单地疯了。

What does any other element on the page have a name attribute that is the same as the id? 页面上的任何其他元素具有与id相同的name属性是什么?

Internet Explorer 8 and later. Internet Explorer 8及更高版本。 In IE8 mode, getElementById performs a case-sensitive match on the ID attribute only. 在IE8模式下,getElementById仅对ID属性执行区分大小写的匹配。 In IE7 mode and previous modes, this method performs a case-insensitive match on both the ID and NAME attributes, which might produce unexpected results. 在IE7模式和以前的模式中,此方法对ID和NAME属性执行不区分大小写的匹配,这可能会产生意外结果。 - http://msdn.microsoft.com/en-us/library/ms536437%28VS.85%29.aspx - http://msdn.microsoft.com/en-us/library/ms536437%28VS.85%29.aspx

Try alerting your the nodeName and id ofr the returned element and make sure its the input you expect. 尝试提醒您返回的元素的nodeName和id,并确保它是您期望的输入。

Is the document's id actually "textFieldID" or is "textFieldID" a javascript variable that contains the ID of the text input to change? 文档的id实际上是“textFieldID”还是“textFieldID”是一个包含要更改的文本输入ID的javascript变量? If it is not a variable, I believe you should make it: 如果它不是变量,我相信你应该成功:

document.getElementById('textFieldID').value=newValue;

It's hard to debug this without the context, since the code you have ought to work fine. 没有上下文就很难调试它,因为你应该使用的代码可以正常工作。 Can you confirm that you've got the right node by doing something like: 你能通过以下方式确认你有正确的节点吗?

document.getElementById(textFieldID).style.border = "4px solid red";

Use div element instead of textfield. 使用div元素而不是textfield。 I had same problem, my textfield which is changed with another script wasnt get the right value. 我有同样的问题,我用另一个脚本改变的文本域没有得到正确的值。 you can easily use any div element like textfield with some CSS. 你可以轻松地使用任何div元素,如textfield和一些CSS。 than you can get the value from div using innerHTML. 你可以使用innerHTML从div获得值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM