如何使用 jQuery 更改只读文本框的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6668364/
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
How to change the value of a readonly textbox using jQuery?
提问by user838697
How can I change the value of a HTML textbox which has readonly property using jQuery?
如何使用 jQuery 更改具有只读属性的 HTML 文本框的值?
回答by Phil
Wait, did you want to remove the read only? If so:
等等,你想删除只读吗?如果是这样的话:
$('textbox').removeAttr('readonly').val('Changed Value');
if not:
如果不:
$('textbox').val('Changed Value');
回答by Greg Guida
$('my-textbox').val('new value')
The read only property only applies to the user viewing the page, not the JavaScript that accesses. it
只读属性仅适用于查看页面的用户,而不适用于访问的 JavaScript。它
回答by Claudio Oyarzùn
older post but here′s some info.
较旧的帖子,但这里有一些信息。
if u have a readonly input (with id test), you just set the val with jquery
如果你有一个只读输入(带有 id 测试),你只需用 jquery 设置 val
$('#test').val(100);
but if you look into html source code in your browser and search in your recently update input value, u will see value empty.
但是如果您在浏览器中查看 html 源代码并搜索您最近更新的输入值,您将看到值为空。
<input type="text" id="test" value />
so, to prevent this, you have to remove readonly atribute before set the value to the input and then set readonly to true again.
因此,为了防止这种情况,您必须在将值设置为输入之前删除 readonly 属性,然后再次将 readonly 设置为 true。
$('#test').prop('readonly',false);
$('#test').val(100);
$('#test').prop('readonly',true);
and the result in html
结果在 html 中
<input type="text" id="test" value="100" />
this prevent error when u need to send form values...
当您需要发送表单值时,这可以防止错误...
回答by AlienWebguy
$('input[readonly]').val('my new val');
回答by Feikn D'baffi
Don't forget to write the # For example
不要忘记写#例如
$('#my-textbox').val('new value');
And maybe you can go through Then after everything don't forget to try using "" instead of ''
也许你可以通过然后在一切之后不要忘记尝试使用 "" 而不是 ''