How to change the value of a readonly textbox using jQuery?
How can I change the value of a HTML textbox which has readonly prop开发者_JAVA百科erty using jQuery?
Wait, did you want to remove the read only? If so:
$('textbox').removeAttr('readonly').val('Changed Value');
if not:
$('textbox').val('Changed Value');
$('my-textbox').val('new value')
The read only property only applies to the user viewing the page, not the JavaScript that accesses. it
older post but here´s some info.
if u have a readonly input (with id test), you just set the val with jquery
$('#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.
<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.
$('#test').prop('readonly',false);
$('#test').val(100);
$('#test').prop('readonly',true);
and the result in html
<input type="text" id="test" value="100" />
this prevent error when u need to send form values...
$('input[readonly]').val('my new val');
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 ''
I ran into a case where it wasn't updating because I was using a datepicker on the textbox; so I had to use something like this to "trigger" the update:
$('#Date').val(date).datepicker('update', date)
I was also using the readonly attribute, but that had nothing to do with it.
精彩评论