jquery how to detect the change of a hidden field
I need to detect when a hidden field is changed. I would like to do some actions each time the value of this hidden changes
I`ve read jquery documentation and found the change(fn), but it says it would only trigger when the focus is released.
would that work fo开发者_如何学Cr a hidden field? I think hidden fields do not have a focus event
any help will be greatly appreciated thanks
You could set a short interval and check for the value like this:
var defaultValue = '';
setInterval(function() {
if ($('.input').val() != defaultValue) {
defaultValue = $('.input').val();
//do something now that the value has changed
}
}, 100);
The .change() function is called when you actually change something you have the focus on. You can't possibly have focus on an hidden field. You should add you script here for us to see what you actually wanna do and what's wrong with it.
You already need to access the hidden field to change the value of it. As @Michael Copeland said, if you performs this change with jquery you can handle this changing in the same place.
Let's say you change the value of a hidden field by pressing a button and when this happen you have to write the value of the field in a div. Here's the solution for this example:
HTML:
<input id="inp" type="hidden" value="0" />
<div id="div"></div>
<button id="butt">Change the value of the hidden input</button>
JS:
$(document).ready(function(){
//this button toggles the value between 1 and 0
$("#butt").click(function(){
var val = ($("#inp").val()=="1")? "0" : "1" ;//choose 1 or 0
$("#inp").val(val);//set the value to the hidden input
setDiv();//calls the handler of the change
});
function setDiv(){
$("#div").html($("#inp").val());
}
});
You can test it here: http://jsfiddle.net/aETUJ/
Hope this helps. :)
精彩评论