Javascript 检测隐藏表单字段中的更改
我正在写一个测试代码来做一个计数器,它将值存储在一个隐藏的表单字段中。每当通过单击按钮增加此计数器时,计数器值将存储在隐藏字段中。这一部分我没有问题 但是,每当隐藏字段被更改时,我都无法显示警报。请看下面我的代码,告诉我哪里出了问题。多谢各位Javascript 检测隐藏表单字段中的更改,javascript,jquery,html,Javascript,Jquery,Html,我正在写一个测试代码来做一个计数器,它将值存储在一个隐藏的表单字段中。每当通过单击按钮增加此计数器时,计数器值将存储在隐藏字段中。这一部分我没有问题 但是,每当隐藏字段被更改时,我都无法显示警报。请看下面我的代码,告诉我哪里出了问题。多谢各位 <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
function startRolling() {
var storage=document.getElementById('store').value;
var tonum;
if(parseInt(storage)==0)
{
tonum=1;
}
else {
tonum=parseInt(storage,10);
}
tonum=tonum+1;
document.getElementById('store').value=tonum;
storage=document.getElementById('store').value;
alert(storage)
}
$(document).ready(function() {
var content = $('#store').val();
$('#store').change(function() {
if ($('#store').val() != content) {
alert('Content has been changed')
}
});
函数startRolling(){
var storage=document.getElementById('store').value;
扁桃体变种;
if(parseInt(存储)==0)
{
张力=1;
}
否则{
tonum=parseInt(存储,10);
}
张力=张力+1;
document.getElementById('store')。value=tonum;
存储=document.getElementById('store')。值;
警报(存储)
}
$(文档).ready(函数(){
var content=$('#store').val();
$('#store').change(function(){
if($('#store').val()!=content){
警报('内容已更改')
}
});
}))
试试这个
function startRolling() {
var storage=document.getElementById('store').value;
var tonum;
if(parseInt(storage)==0)
{
tonum=1;
}
else {
tonum=parseInt(storage,10);
}
tonum=tonum+1;
document.getElementById('store').value=tonum;
//storage=document.getElementById('store').value;
//alert(storage)
}
$(document).ready(function() {
//var content = $('#store').val();
$('#store').change(function() {
//if ($('#store').val() != content) {
alert('Content has been changed')
}
});
为什么不将第一个函数更改为jquery?从:当控件失去输入焦点且自获得焦点后其值已被修改时,将发生更改事件 隐藏的输入不能失去焦点(因为它们从来没有焦点),所以无论如何都不会触发更改
有关解决方案,请参阅。我使用每秒检查一次的循环事件,而不是使用更改事件
var content="";
function startRolling() {
var storage=document.getElementById('store').value;
var tonum;
if(parseInt(storage)==0)
{
tonum=1;
}
else {
tonum=parseInt(storage,10);
}
tonum=tonum+1;
document.getElementById('store').value=tonum;
storage=document.getElementById('store').value;
content=storage;
alert(storage)
}
function checkifchanged(){
if ($('#store').val() != content) {
alert('Content has been changed');
}
else{
content = $('#store').val();
}
setTimeout('checkifchanged()',1000);
}
$(document).ready(function() {
content = $('#store').val();
checkifchanged();
});
如果您只是触发事件而不尝试检测更改,该怎么办
function startRolling() {
var storage=document.getElementById('store').value;
var tonum;
if(parseInt(storage)==0)
{
tonum=1;
}
else {
tonum=parseInt(storage,10);
}
tonum=tonum+1;
document.getElementById('store').value=tonum;
if(storage != tonum) {
alertChange();
}
//storage=document.getElementById('store').value;
//alert(storage)
}
function alertChange() {
alert('Content has been changed');
}
您还可以查看jquery中的触发器事件:
您正在存储更改的值并与相同的值进行比较,
此if语句不执行
if ($('#store').val() != content)
不要存储值,直接调用change事件即可
$(document).ready(function() {
$('#store').change(function() {
alert('Content has been changed')
});
如果他们要手动编辑一个隐藏的表单字段,是什么让你觉得他们不能简单地删除那段代码,这样它就不会被检查?这段代码只是我开发的一个更大程序的一小部分。实际的程序不会发布在internet上,而只是发布在本地主机上,以实现一次性例行任务。尝试过之后,仍然不会触发警报。不管怎样,谢谢。我想我已经浪费了太多的时间在一个简单的解决方案上了。我现在不在电脑旁,但我认为这应该可以正常工作。我将对此进行测试并发布我的解决方案。非常感谢。
if ($('#store').val() != content)
$(document).ready(function() {
$('#store').change(function() {
alert('Content has been changed')
});