Input value doesn't display. How is that possible?

◇◆丶佛笑我妖孽 提交于 2019-12-03 10:30:03

问题


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:

<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:

<?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.

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.


回答1:


Argh. I knew this 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.

Thanks for your help, everyone.




回答2:


Mine was related to AngularJS

I was trying to put both an 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...

So the answer was to assign that default value to the $scope.variable in the controller.




回答3:


For me it was browser remembered. Changing url string or clearing history can help.




回答4:


For my side, it was a problem only for Firefox.

I resolved by adding the attribute autocomplete="off" in the input field.

<input type="text" value="value must appear" autocomplete="off"/>



回答5:


Are you confusing the uses of the 'default' and the 'value' parameters for $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.

If you want to force display of a value, you should use the 'value' parameter instead. 'value' => $product['Product']['make']




回答6:


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.

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.




回答7:


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.




回答8:


I even set autocomplete to "off" with no result. I ended up putting the next jquery snippet at the document.ready event.

myForm.find("input").each((i, el) => {
  $(el).val($(el).attr("value"));
});

Adittionally, this would be the equivalent in pure 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.




回答9:


For me it was because I was using the <input> tag without enclosing it inside a <form> tag




回答10:


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.



来源:https://stackoverflow.com/questions/5772124/input-value-doesnt-display-how-is-that-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!