检测文本输入的值是否随着事件而改变 [英] Detect if the value of a text input changes with no event
问题描述
如果它发生变化,我希望得到这个输入的值。我有一个map.swf,使得值根据用户在.swf中选择的内容而改变,所以我想知道如何完成这个。
< a href =map.phponclick ='return GB_showCenter(,this.href,380,780);'class =page>查看地图< / a>
< input name =sector_etype =textid =sector_evalue =请点击地图size =1readonly>
这个jQuery只会提醒我,如果我按下某个键,我希望它在警告任何事件。
$ b $
$('#sector_e')。bind(keyup,function(){
alert($ this).val());
});
您需要做一些事情的组合:
即使您选择,也取决于直接用户交互是否可行。一旦检测到更改,可能会发生进一步的处理。 b
$ b
例如:
你可能会考虑使用类名isDirty来使用jQuery的< a href =http://api.jquery.com/addClass/ =nofollow> addClass
命令。
然后,您可以执行以下操作:
$('。isDirty')
与此同时......
这里是使用FOCUSOUT的代码样本:
< script type =text / javascript> ;
$(document).ready(function(){
var txtSectorE = $('#sector_e');
//保存初始值
$。 data(txtSectorE,last,txtSectorE.val());
//设置事件
txtSectorE.focusout(function(){
var last = $ .data (txtSectorE,last);
if(last!= $(this).val())
alert(changed);
});
}) ;
< / script>
< input id =sector_ename =sector_etype =textvalue =请点击地图>
当然,这样做会导致重复征税,因为您必须编写一组单独的调用每个文本框
。
所以,为什么不给每个文本框添加一个类名 code>并以这种方式执行...
一个使用FOCUSOUT的更好示例:
< script type =text / javascript>
$(document).ready(function(){
var txtSectors = $('。sector');
$ .each(txtSectors,function(index,textbox) {
//保存初始值
$ .data(textbox,last,textbox.val());
//设置事件
textbox.focusout(function(){
var element = $(this);
var last = $ .data(element,last);
if(last!= element.val())
alert(changed);
});
});
});
< / script>
< input id =sector_atype =textclass =sectorvalue =Something>
< input id =sector_btype =textclass =sectorvalue =Another>
< input id =sector_ctype =textclass =sectorvalue =Yet Again>
< input id =sector_dtype =textclass =sectorvalue =Wow This Arduous>
< input id =sector_etype =textclass =sectorvalue =正在厌倦输入>
I'm looking to get the value of this input if it changes. I have a map.swf that makes the value change depending on what the user selects in the .swf, so I was wondering how to get this done.
<a href="map.php" onclick='return GB_showCenter("",this.href,380,780);' class="page">See Map</a>
<input name="sector_e" type="text" id="sector_e" value="Please click The Map" size="1" readonly>
This jQuery only alerts me if I press a key, I want it to alert if the value changes without any event.
$('#sector_e').bind("keyup", function() {
alert($(this).val());
});
You will need to do a combination of things:
- Use jQuery's
data
command capabilities to save the initial value. - Use the jQuery's
focusout
orblur
orchange
event to check the current value against what is saved usingdata
.
Which even you choose depends on whether or not direct user-interaction is possible or not. Further processing can happen once you detect a change.
FOR EXAMPLE:
You might consider marking the element with a class name like "isDirty" using jQuery's addClass
command.
Then you could do things like:
$('.isDirty')
In the meantime...
HERE IS A CODE SAMPLE USING FOCUSOUT:
<script type="text/javascript">
$(document).ready(function () {
var txtSectorE = $('#sector_e');
// Save the initial value
$.data(txtSectorE, "last", txtSectorE.val());
// Setup the event
txtSectorE.focusout(function () {
var last = $.data(txtSectorE, "last");
if (last != $(this).val())
alert("changed");
});
});
</script>
<input id="sector_e" name="sector_e" type="text" value="Please click The Map">
Of course, doing it this way is taxing as you would have to write a separate set of calls for each textbox
.
So, why not add a class name to each textbox
and do it this way...
A BETTER EXAMPLE USING FOCUSOUT:
<script type="text/javascript">
$(document).ready(function () {
var txtSectors = $('.sector');
$.each(txtSectors, function (index, textbox) {
// Save the initial value
$.data(textbox, "last", textbox.val());
// Setup the event
textbox.focusout(function () {
var element = $(this);
var last = $.data(element, "last");
if (last != element.val())
alert("changed");
});
});
});
</script>
<input id="sector_a" type="text" class="sector" value="Something">
<input id="sector_b" type="text" class="sector" value="Another">
<input id="sector_c" type="text" class="sector" value="Yet Again">
<input id="sector_d" type="text" class="sector" value="Wow This Arduous">
<input id="sector_e" type="text" class="sector" value="Getting Tired Of Typing">
这篇关于检测文本输入的值是否随着事件而改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!