![](/img/trans.png)
[英]How to check whether the input name contains specific value using if loop in jquery
[英]How to check if there exist an input with specific name attr using jQuery
我需要一个函数来使用jQuery动态添加新的输入元素。
就是这样的
function addInput(inputName) {
$('<input>', {
type: "hidden",
name: "inputName"
}).appendTo('#foo');
}
addInput('test');
并且它有效,它为#foo
添加了一个新的<input type="hidden" name="test">
只有在DOM
中不存在具有指定名称的输入时,才会出现问题。
这是我更新的功能
function addInput(inputName) {
if (! $(input).attr('name','inputName') {
$('<input>', {
type: "hidden",
name: "inputName"
}).appendTo('#foo')
}
}
addInput('test');
但是,通过名称test
检查input
元素,它设置了要test
的所有input
元素的名称。 有没有方法只测试输入名称?
function addInput(inputName) {
if ($('input[name="'+inputName+'"]').length > 0)
{
alert("Exists");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.