按类型和名称输入的目标(选择器) [英] target input by type and name (selector)

查看:140
本文介绍了按类型和名称输入的目标(选择器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将某些复选框输入更改为页面上部分而非全部输入的隐藏输入.

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">

下面的jquery代码仅按类型选择输入,这会导致所有复选框都变为隐藏的输入.有没有一种方法可以检查input ="checkbox"和name ="ProductCode"的类型作为选择器,所以我可以专门针对那些我想要更改的对象?

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 selector

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

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

最好使用CSS类来标识要选择的CSS,但是由于许多现代浏览器实现了document.getElementsByClassName方法,该方法将用于选择元素,并且比name属性

It would be better to use a CSS class to identify those that you want to select however as a lot of the modern browsers implement the document.getElementsByClassName method which will be used to select elements and be much faster than selecting by the name attribute

这篇关于按类型和名称输入的目标(选择器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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