I need to change some checkbox inputs to hidden inputs for some but not all inputs on a page.

<input type="checkbox" name="ProductCode"value="396P4"> 
<input type="checkbox" name="ProductCode"value="401P4"> 
<input type="checkbox" name="ProductCode"value="F460129">

The jquery code below only selects the input by type which causes all check boxes to changed to hidden inputs Is there a way to check for both type of input="checkbox" and name="ProductCode" as the selector so I can specifically target those that I want to change?

$("input[type='checkbox']").each(function(){
var name = $(this).attr('name'); // grab name of original
var value = $(this).attr('value'); // grab value of original
var html = '<input type="hidden" name="'+name+'" value="'+value+'" />';
$(this).after(html).remove(); // add new, then remove original input
});

推荐答案

You want a multiple attribute select或

$("input[type='checkbox'][name='ProductCode']").each(function(){ ...

$("input:checkbox[name='ProductCode']").each(function(){ ...

不过,最好使用CSS类来识别要 Select 的元素,因为许多现代浏览器都实现了document.getElementsByClassName方法,该方法将用于 Select 元素,并且比使用name属性 Select 要快得多

Jquery相关问答推荐

如何在 jQuery 中使用:not 和 hasClass() 来获取没有类的特定元素

在 for 循环中分配点击处理程序

如何使用 jQuery 重置 Select 元素

如何在循环内创建动态变量名称?

在 Javascript 中,字典理解或 Object `map`

如何在 Jquery 中将 delay() 与 show() 和 hide() 一起使用

如何获取 onclick 调用对象?

延期与promise

使用 jQuery 从 AJAX 响应(json)构建表行

动态创建并提交表单

我想了解 jQuery 插件语法

Bootstrap 4 文件输入

如何通过jQuery函数仅获取直接子元素

jQuery确定匹配的类是否具有给定的ID

如何在 jQuery 中 Select this中的元素?

PHPStorm IDE 中低效的 jQuery 使用警告

IE8 和 JQuery 的 trim()

你会如何比较 jQuery 对象?

如何使用 ID 变量在 jQuery 中 Select 元素?

如何判断值是否为 JSON 对象?