Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery在不丢失其值的情况下操作动态创建的表单元素_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery在不丢失其值的情况下操作动态创建的表单元素

Javascript 使用jQuery在不丢失其值的情况下操作动态创建的表单元素,javascript,jquery,Javascript,Jquery,我想对一个复杂的动态创建的表单进行搜索和替换。这就像查找一个字符串的所有实例并用另一个字符串替换它们一样简单,更复杂的是,每一位表单有11个实例,并且只构成各种属性的一部分 我正在使用的类型的示例(我正在搜索和替换的是'001'位): 但是,由于表单位是动态添加的,因此每次执行此操作时表单数据都会丢失。我尝试使用.clone()函数,它似乎可以保留数据,但不允许您操作对象。我还尝试直接对包含的对象执行搜索和替换,但我猜.replace()只对字符串有效 您可以只更新属性吗 $('div, lab

我想对一个复杂的动态创建的表单进行搜索和替换。这就像查找一个字符串的所有实例并用另一个字符串替换它们一样简单,更复杂的是,每一位表单有11个实例,并且只构成各种属性的一部分

我正在使用的类型的示例(我正在搜索和替换的是'001'位):


但是,由于表单位是动态添加的,因此每次执行此操作时表单数据都会丢失。我尝试使用.clone()函数,它似乎可以保留数据,但不允许您操作对象。我还尝试直接对包含的对象执行搜索和替换,但我猜.replace()只对字符串有效

您可以只更新属性吗

$('div, label, input').each(
    function() {
        var _this = $(this);
        _this.attr('id', _this.attr('id').replace('old string', 'new string'));
        _this.attr('for', _this.attr('id').replace('old string', 'new string'));
        _this.attr('name', _this.attr('id').replace('old string', 'new string'));
    }
);

可能有点冗长…

使用.live()函数应该可以解决这个问题。这个函数是使用onclick调用的,所以我不确定.live()会去哪里。好的,在一些不相关的问题之后,由于不正确的字符(条和冒号),我已经开始工作了。问题是它不能很好地处理名称和属性,如果节点没有它们,它就会失败。我添加了检查,看看它们是否存在,从而解决了这个问题。是的,对不起,这是未经测试的,只是一个想法/指针,即我没有添加错误检查。很高兴它还是有用的:)
$(this).html($(this).html().replace('old string', 'new string'));
$('div, label, input').each(
    function() {
        var _this = $(this);
        _this.attr('id', _this.attr('id').replace('old string', 'new string'));
        _this.attr('for', _this.attr('id').replace('old string', 'new string'));
        _this.attr('name', _this.attr('id').replace('old string', 'new string'));
    }
);