jQuery changing the value of a hidden field
function areaMe(area){
var barea = $('#barea').val();
if(barea.indexOf(area)!=-开发者_StackOverflow社区1){
barea=barea.replace(area, ""); // remove
}else{
barea+=area; // inlcui
}
$('#barea').val(barea);
}
It doesn't work if the #barea input is hidden...
For a hidden input I always use
var barea = $('#barea').attr('value');
$('#barea').attr('value',barea);
not val(). I've never had luck changing the value of an input type=hidden
any other way.
If barea
isn't a input type=hidden
, then you'll need to include the html for it. Note that it must be some type of input as val() only works on inputs, selects, and textareas.
You need to provide more info. I've always been able to use val() on hidden inputs (in the type="hidden" sense, although I tested with "disply:none" ones too and they also worked). I even tested one just now, and sure enough it worked with both the "get" and "set" forms of val(). So while .attr("value") might work, it really shouldn't be needed. Can you please provide:
A) Which of the two forms is failing for you (the "get" or the "set" form)?
B) The HTML which defines your hidden input?
精彩评论