Py学习  »  Jquery

组合框交互中的jquery live方法

ronk • 5 年前 • 745 次点击  

我正试图在jquery中做下一件事。 我有两个组合框,我想确保它们的选择值是相同的。如果用户在一个组合框中选择一个值,就像在另一个组合框中一样,我想提醒“无效操作”,并将组合框选择的值设置为其上一个值。 所以我写道:

$("#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相同

你有更好的主意吗 谢谢你

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/42901
 
745 次点击  
文章 [ 2 ]  |  最新文章 5 年前
zincorp
Reply   •   1 楼
zincorp    15 年前

$(“selectGroupMargin”)与该函数上下文中的$(this)相同。使用“更改”进行现场通话

Russ Cam
Reply   •   2 楼
Russ Cam    15 年前

试用使用 change onchange live() 方法。另外,如果我的内存正确地为我服务,您将需要jquery 1.4+来使用 live 对于change事件,因为代码仅在1.4+中实现,以处理 改变 事件。

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