组合框交互中的 jquery live 方法

发布于 2024-08-23 01:42:08 字数 820 浏览 4 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒几许 2024-08-30 01:42:08

尝试在 live() 方法中使用 change 而不是 onchange。另外,如果我没记错的话,您需要 jQuery 1.4+ 才能将 live 与更改事件一起使用,因为代码仅在 1.4+ 中实现来处理 change 的委托代码>事件。

在将新选择的值与上次选择的值进行比较时,您可以考虑使用 $.data() - 然后您可以将更改事件后的值与存储在 $.cache 中的值(其中 $.data() 存储值)进行比较,然后执行必要的。您也可以使用闭包来实现它。

Try using change as opposed to onchange in the live() method. Also, if my memory serves me correctly, you'll need jQuery 1.4+ to use live with the change event, as code was only implemented in 1.4+ to handle delegation for the change 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.

×纯※雪 2024-08-30 01:42:08

$("#SelectGroupMargin") 与该函数上下文中的 $(this) 相同。并使用“更改”进行实时通话

$("#SelectGroupMargin") is the same as $(this) in the context of that function. And use "change" for the live call

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文