Html 输入值不显示。这怎么可能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5772124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input value doesn't display. How is that possible?
提问by Rob Wilkerson
This must be something utterly stupid that I've done or am doing, but I have an input with a value attribute that simply isn't being displayed:
这一定是我做过或正在做的非常愚蠢的事情,但我有一个输入,其 value 属性根本没有显示:
<div class="input text required">
<label for="Product0Make">Make</label>
<input name="data[Product][0][make]" type="text" maxlength="255" value="AC Make" id="Product0Make">
</div>
Has anyone ever seen this before? Do I have some kind of typo that I'm just blind to? For whatever it may be worth, here's the CakePHP code that's generating this line:
有没有人见过这个?我是否有某种我视而不见的错字?不管它值多少钱,下面是生成这一行的 CakePHP 代码:
<?php echo $this->Form->input( 'Product.' . $index . '.make', array( 'default' => $product['Product']['make'] ) ) ?>
I have a small form with a handful of text inputs, 1 textarea and 2 selects. None of the text input values display, but everything else is fine.
我有一个带有少量文本输入、1 个文本区域和 2 个选择的小表单。没有显示文本输入值,但其他一切都很好。
Any thoughts would be appreciated. I can't even believe I'm having to ask this question, but that's how crazy it's making me.
任何想法将不胜感激。我什至不敢相信我不得不问这个问题,但这让我很疯狂。
回答by Rob Wilkerson
Argh. I knewthis was going to be something beyond stupid. There was a bit of Javascript that was clearing data. The clearing was useful in other places, but I didn't know it was executing so it was a serious pain to track down. Once I prevented the clearing in this scenario, my values actually appeared. Because I was looking at the code in web inspector, I assumed that it would be a "live" view, but I guess that's not entirely true.
啊。我知道这将是一种超越愚蠢的事情。有一些 Javascript 正在清除数据。清理在其他地方很有用,但我不知道它正在执行,所以追踪起来很痛苦。一旦我阻止了这种情况下的清算,我的价值观就会真正出现。因为我正在查看 Web 检查器中的代码,所以我认为它是一个“实时”视图,但我想这并不完全正确。
Thanks for your help, everyone.
谢谢大家的帮助。
回答by Suamere
Mine was related to AngularJS
我的与AngularJS有关
I was trying to put bothan HTML Value and an ng-Model, thinking that the ng-Model would default to the Value, because I was too lazy to add the Model to the $scope in the Controller...
我试图把双方的HTML值和NG-型号,以为NG-模型将默认值,因为我懒得模型添加到控制器中的$范围...
So the answer was to assign that default value to the $scope.variable in the controller.
因此,答案是将该默认值分配给控制器中的 $scope.variable。
回答by J.BizMai
For my side, it was a problem only for Firefox.
对我来说,这只是 Firefox 的问题。
I resolved by adding the attribute autocomplete="off"
in the input field.
我通过autocomplete="off"
在输入字段中添加属性来解决。
<input type="text" value="value must appear" autocomplete="off"/>
回答by Fanky
For me it was browser remembered. Changing url string or clearing history can help.
对我来说,它是浏览器记住的。更改 url 字符串或清除历史记录会有所帮助。
回答by Tyler
Are you confusing the uses of the 'default' and the 'value' parameters for $html->input()?
您是否混淆了 $html->input() 的“默认”和“值”参数的使用?
If you're are using 'default' => $product['Product']['make']
and $this->data is present, the field will not be populated. The purpose of the 'default' parameter is to display a default value when no form data ($this->data) is present.
如果您正在使用'default' => $product['Product']['make']
并且 $this->data 存在,则不会填充该字段。'default' 参数的目的是在不存在表单数据 ($this->data) 时显示默认值。
If you want to force display of a value, you should use the 'value' parameter instead. 'value' => $product['Product']['make']
如果要强制显示值,则应改用 'value' 参数。 'value' => $product['Product']['make']
回答by chakeda
For Googler's who may have the same issue: This can happen if you have a non-numeric value in a number
type input field.
对于可能有相同问题的 Google 员工:如果您在number
类型输入字段中有非数字值,就会发生这种情况。
For example:
例如:
<input type="number" value="<? echo $myNumberValue; ?> "/>
This will show nothing even though Dev tools says the value is there, since the extra space after ?>
makes it non-numeric. Simply remove the extra space.
即使开发工具说该值存在,这也不会显示任何内容,因为后面的额外空间?>
使其成为非数字。只需删除多余的空间。
回答by marco bonfigli
Had a similar problem with input value retrieved via ajax, correctly set and verifiable via browser console, but not visible. The issue was another input field having the same id, and it was not evident because of several JSP files included, many of them having forms.
通过 ajax 检索的输入值存在类似问题,通过浏览器控制台正确设置和验证,但不可见。问题是另一个具有相同 id 的输入字段,并且由于包含多个 JSP 文件而并不明显,其中许多文件具有表单。
回答by Fabio Sungurlian
I even set autocomplete to "off" with no result. I ended up putting the next jquery snippet at the document.ready event.
我什至将自动完成设置为“关闭”而没有结果。我最终将下一个 jquery 片段放在 document.ready 事件中。
myForm.find("input").each((i, el) => {
$(el).val($(el).attr("value"));
});
Adittionally, this would be the equivalent in pure es2015:
另外,这在纯 es2015 中是等价的:
document.querySelectorAll("myForm input").forEach(el => {
el.value = el.getAttribute("value");
});
If your not using a precompilor like Babel and you need compatibility for old browser's versions, change the "(el) =>" for "function(el)". I tried both codes in my scenario and worked fine.
如果您不使用像 Babel 这样的预编译器,并且需要与旧浏览器版本兼容,请将“(el) =>”更改为“function(el)”。我在我的场景中尝试了这两种代码并且工作正常。
回答by Kewal Shah
For me it was because I was using the <input>
tag without enclosing it inside a <form>
tag
对我来说,这是因为我在使用<input>
标签时没有将其包含在<form>
标签中
回答by user6096790
For me the problem was that I had multiple inputs with the same id. I could see the value in the field, but reading it via javascript gave an empty value because it was reading a different input with the same id - I was surprised that there was no javascript error, just could not read the values I could see in the form.
对我来说,问题是我有多个具有相同 ID 的输入。我可以看到字段中的值,但是通过 javascript 读取它给出了一个空值,因为它正在读取具有相同 ID 的不同输入 - 我很惊讶没有 javascript 错误,只是无法读取我可以看到的值表格。