Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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/3/clojure/3.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
改变多输入';s id';他正在使用jquery_Jquery_Html - Fatal编程技术网

改变多输入';s id';他正在使用jquery

改变多输入';s id';他正在使用jquery,jquery,html,Jquery,Html,我正在尝试更改父容器内所有复选框的id,但到目前为止,我无法使其正常工作,这是我的代码 HTML: 你知道这有什么问题吗 Thanx提前。数据自定义='other_container'元素没有任何属于元素的子元素 你应该 .find()方法将搜索所有子代,只查看直接子代 但请记住,ID必须是唯一的,因此您需要确保为每个ID分配一个唯一的ID。你可以这样做: $("[data-custom='other_container']").find('input').attr('id', function

我正在尝试更改父容器内所有复选框的id,但到目前为止,我无法使其正常工作,这是我的代码

HTML:

你知道这有什么问题吗


Thanx提前。

数据自定义='other_container'元素没有任何属于
元素的子元素

你应该

.find()
方法将搜索所有子代,只查看直接子代

但请记住,ID必须是唯一的,因此您需要确保为每个ID分配一个唯一的ID。你可以这样做:

$("[data-custom='other_container']").find('input').attr('id', function( i ) {
       return 'test' + i;
});
此外,在本例中,最好在选择器中指定
div
标记名,这样jQuery就不会查看
数据自定义属性的每个元素

$("div[data-custom='other_container']")
您还可以通过将
.container
类添加到选择器中,使其更加具体

$("div.container[data-custom='other_container']")

数据自定义
没有任何属于

你可以用这个代替-

$("[data-custom='other_container'] input").attr('id', 'test');
上述代码将搜索所有儿童、大儿童等的
,并替换其id。您使用的代码仅搜索
数据自定义的儿童

您的代码相当于-

$("[data-custom='other_container'] > input").attr('id', 'test');
jQuery只选择所选元素的子元素;它不会抓取所有的后代(即,孩子的孩子,他们的孩子,等等),这就是你想要的。在你的例子中

$("[data-custom='other_container']").children()
…仅获取其他容器的子级,即h3和div.container元素。添加“输入”过滤器将根本不选择任何元素

要查找特定类型的所有子体,请使用jQuery的。您的代码可以重写为:

$("[data-custom='other_container']").find('input')
最后,请记住,所有元素ID都必须是唯一的。如果他们不是,尝试$(“#此id不是唯一的”)之类的东西会让你相当沮丧。如果需要对每个输入应用相同的标识符,请考虑向每个元素添加一个类。

$("[data-custom='other_container'] > input").attr('id', 'test');
$("[data-custom='other_container']").children()
$("[data-custom='other_container']").find('input')