使用JQuery恢复Html,除了一些元素

Restore Html with JQuery, except some elements

本文关键字:元素 JQuery 恢复 Html 使用      更新时间:2023-09-26

在Ajax请求之后,我想将我的表单元素重置为它们的原始值,除了三个选择输入,因为它们的值将被频繁使用。

我在"document ready"外声明了这个变量:

var estado_inicial_cuerpo;

我在"document ready"函数中尝试了这个:

estado_inicial_cuerpo=$("#formulario_venta").clone().children('#id_bodega_idbodega').remove().children('#id_caja_idcaja').remove().end();

在ajax的"success"部分(或者,如果你喜欢,在某个激活此代码的按钮中),这个:

$('#formulario_venta').html(estado_inicial_cuerpo);

但是,在控制台中,我可以看到使用该代码加载了两次页面,并且整个表单变为空白,所有元素都消失了。

我的表单的ID是formulario_ventaid_caja_idcaja, id_bodega_idbodega是我想跳过的选择输入的ID。


现在我在"document ready"代码中尝试了这个:

estado_inicial_cuerpo=$("#formulario_venta").not("#id_bodega_idbodega, #id_caja_idcaja").html();

现在表单没有消失,但是所有的东西都恢复了,包括我写在"not"里面的元素。

你试过了吗:

$('#formulario_venta').find('input, textarea, select').not("#id_bodega_idbodega, #id_caja_idcaja").val("");

也就是说,除了.not()

里面的那个,所有的输入都是value = ""(空)

.find('input, textarea')是您的表单可能具有的输入类型

试试

estado_inicial_cuerpo = $("#formulario_venta").html()
你们有样品吗?

首先从元素中清除html,然后重新赋值。

$('#formulario_venta').html('');
$('#formulario_venta').html(estado_inicial_cuerpo);