why i cannot replace value of a hidden input?
i have a hidden input like this:
<input type="hidden" id="selectedItem" value="1,1,1," name="selectedItem"/>
and i want to replace some values of it to:
value="9,9,9"
i tried to used
document.getElementById('selectedItem').value = document.getElementById('selectedItem').value.re开发者_JAVA百科place(/1/gi,'9')
or
document.getElementById('selectedItem').value = document.getElementById('selectedItem').value.replace('1','9')
but it didnot work. Please someone tells me why and give me some solutions. Thank You
Why not use jquery? Something like the following:
$('input#selectedItem').value('9,9,9');
$("#selectedItem").val("9,9,9");
//or
$("#selectedItem").val($("#selectedItem").val().replace(/1/gi,'9'));
It works just fine for me: http://jsfiddle.net/QRHTL/
It would be a bit cleaner with jQuery though:
$('#selectedItem').val($('#selectedItem').val().replace(/1/gi,'9'));
Do you have another element on your page with id="selectedItem"
? If so, document.getElementById("selectedItem")
might return that instead of the hidden form input you're expecting it to.
精彩评论