![](/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.