使两个< select>保持同步 [英] Keep two <select>s in sync

查看:53
本文介绍了使两个< select>保持同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有两个<select>,但屏幕不同.它们具有附加的相同change处理函数,并且具有相同的值.当一个更改时,另一个应相应地更改其值.

I have two <select>s in my app but on different screens. They have the same change handler attached to them and have the same values. When one changes the other should change its value accordingly.

问题是当一个更改时,我尝试更改另一个,它将在第一个事件上触发change事件,依此类推.

The problem is when one is changed and I try to change the other, it will fire the change event on the first one and so on recursively.

那我该如何更改选择,而不触发更改事件?

So how do I change a select, but not fire a change event?

推荐答案

当我尝试更改另一个事件时,它将在第一个事件上触发change事件,依此类推.

when I try to change the other, it will fire the change event on the first one and so on recursively.

真的吗?然后,您必须做错了什么,通常更改值不会触发该事件.只需使用

Really? You must be doing something wrong then, usually changing the value does not fire the event. Just use

var bothselects = $("#first-select, #second-select");
bothselects.change(function(e) {
    bothselects.val(this.value); // "this" is the changed one

    // do whatever else you need to do
});

这篇关于使两个&lt; select&gt;保持同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆