替换后维护jQuery onChange [英] Maintain jQuery onChange After a Replacewith

查看:119
本文介绍了替换后维护jQuery onChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以抓住页面上的第一个选择并对其进行克隆,以便以后可以根据需要将其重置.

I have some code that grabs the first select on the page and clones it so that I can reset it later on if needed.

mySelect = jQuery('select').get(0);
origSelect = jQuery(mySelect).clone();

jQuery(mySelect).change(function(){
   //some code
}

当我触发代码以重置选择时:

When I fire the code to reset the select with:

jQuery(mySelect).replaceWith(origSelect);

onChange函数停止工作.我已经确定是因为它现在被称为origSelect并响应该名称的onChanges.在这里,我是否有任何选项可以用origSelect的内容替换mySelect的内容,而无需更改其引用的名称?

The onChange function stops working. I've determined that it's because it is now referred to as origSelect and responds to onChanges for that name. Do I have any options here for replacing the content of mySelect with the content of origSelect without changing the name it's referred to by?

推荐答案

将参数true传递给克隆方法.

Pass a parameter true to the clone method.

origSelect = jQuery(mySelect).clone(true); //This will clone the select with data and events

或用mySelect替换origSelect的innerHTMl.

or replace the innerHTMl of origSelect with that of mySelect.

jQuery(origSelect).html(jQuery(mySelect).html()); 

这篇关于替换后维护jQuery onChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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