组合框交互中的 jquery live 方法
我正在尝试用 Jquery 做接下来的事情。 我有 2 个组合框,我想确保它们选择的值是相同的。如果用户在其中一个组合中选择一个值,就像另一个组合中的值一样,我想提醒“无效操作”并将组合所选值设置为其先前的值。 所以我写道:
$("#SelectGroupMargin").live("onchange", function() {
// save the value before the change in case the change is invalid
var valBeforeChange = $("#SelectGroupMargin").val();
var currentLimitedRuleVal = $("#SelectGroup").val();
var newFillerRule= $(this).val();
// check that the new value does not colide with the value of the limited rule
// if it does colide alert the user and return to the former value
if (currentLimitedRuleVal == newFillerRule) {
alert("invalid op");
$("#SelectGroupMargin").text(valBeforeChange);
}
});
但我有几个问题: 1)onchange 没有响应 - 只需单击并聚焦 2) newFillerRule 始终与 valBeforeChange 相同
你有更好的主意/更简短的建议吗 感谢你
I am trying to do the next thing in Jquery.
I have 2 comboboxes and i want to make sure that their selected values is identical. If user chooses a value in one of the combo like the other one, i want to alert "invalid op" and set the combo selected value to its previous value.
so i wrote:
$("#SelectGroupMargin").live("onchange", function() {
// save the value before the change in case the change is invalid
var valBeforeChange = $("#SelectGroupMargin").val();
var currentLimitedRuleVal = $("#SelectGroup").val();
var newFillerRule= $(this).val();
// check that the new value does not colide with the value of the limited rule
// if it does colide alert the user and return to the former value
if (currentLimitedRuleVal == newFillerRule) {
alert("invalid op");
$("#SelectGroupMargin").text(valBeforeChange);
}
});
but i have few problems:
1)onchange doesn't respond - just click and focusout
2) newFillerRule always identical to valBeforeChange
Do you have better idea/shorter any advice
thank u
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在
live()
方法中使用change
而不是onchange
。另外,如果我没记错的话,您需要 jQuery 1.4+ 才能将live
与更改事件一起使用,因为代码仅在 1.4+ 中实现来处理change
的委托代码>事件。在将新选择的值与上次选择的值进行比较时,您可以考虑使用
$.data()
- 然后您可以将更改事件后的值与存储在 $.cache 中的值(其中 $.data() 存储值)进行比较,然后执行必要的。您也可以使用闭包来实现它。Try using
change
as opposed toonchange
in thelive()
method. Also, if my memory serves me correctly, you'll need jQuery 1.4+ to uselive
with the change event, as code was only implemented in 1.4+ to handle delegation for thechange
event.In terms of comparing the newly selected value to the last selected value, you might consider storing the selected value for the using
$.data()
- then you can compare the value after the change event with the value stored in $.cache (where $.data() stores values) and do the necessary. You could also implement it using a closure too.$("#SelectGroupMargin") 与该函数上下文中的 $(this) 相同。并使用“更改”进行实时通话
$("#SelectGroupMargin") is the same as $(this) in the context of that function. And use "change" for the live call