[英]Check if element with same name attribute exists jQuery
I have the following jsFiddle which best demonstrates what I want to do. 我有以下jsFiddle ,它最好地演示了我想做什么。 It is fairly self-explanatory: if an element with the same name attribute exists, do nothing;
这是不言自明的:如果存在具有相同名称属性的元素,则什么都不做; if no such element exists, append that element.
如果不存在这样的元素,请追加该元素。
if (an element exists in the div 'selects' with name="folder3") {
return false;
} else {
$('#selects').append('<select name="folder3"></select>');
}
Kept it short and sweet for this one! 保持简短而甜蜜! Any help would be much appreciated.
任何帮助将非常感激。
Something like: 就像是:
if ($('#selects select[name="folder3"]').length) {
is probably what you're looking for. 可能正是您想要的。
if ($("#selects select[name='folder3']").length>0) {
return false;
} else {
$('#selects').append('<select name="folder3"></select>');
}
if ($('#selects select[name="folder3"]').length) {
return false;
} else {
$('#selects').append('<select name="folder3"></select>');
}
Check this out. 看一下这个。 There are many other work arounds.
还有许多其他解决方法。 Here I check each dropdown, not every other element in the page.
在这里,我检查每个下拉列表,而不是页面中的所有其他元素。 http://jsfiddle.net/Ue7VQ/4/
http://jsfiddle.net/Ue7VQ/4/
$(function ()
{
$.each( $("select"), function() {
if ($(this).attr("name") == "folder3")
{
alert('Do you what you want to do over here');
}
});
});
I answered this same question with the following plugin here . 我在这里用以下插件回答了同样的问题。 Please visit answer for full details on creating of plugin.
请访问答案以获取有关创建插件的完整详细信息。
The following plugin would allow you to use a callback feature (staying inline with jQuery style markup) if the element exist. 如果该元素存在,则以下插件将允许您使用回调功能(与jQuery样式标记保持内联)。 So for your example, you might do something like:
因此,对于您的示例,您可以执行以下操作:
// first callback is "if exist",
// second callback method is "if not exist"
$('#selects select[name="folder3"]').exist(function(exist, elements, index) { // with a param, this cb will always fire
/* Do work for existing elements here */
/* $(this) && this work just like any other jQuery method. this is the current element worked on */
/* Params:
exist: boolean (should always be true here) if exist
elements: complete list of elements called using your selector, in this case '#selects select[name="folder3"]'
index: the index number within the list of elements of THIS element
*/
},
function (exist, elements) {
/* Do alternate work, for elements do not exist */
/* Params:
exist: boolean (should always be false here) if exist
elements: complete list of elements called using your selector, in this case '#selects select[name="folder3"]'
*/
});
| | OR |
或
if (!$('#selects select[name="folder3"]').exist()) {
// element did not exist
$('#selects').append('<select name="folder3"></select>');
}
(function($) {
if (!$.exist) {
$.extend({
exist: function() {
var ele, cbmExist, cbmNotExist;
if (arguments.length) {
for (x in arguments) {
switch (typeof arguments[x]) {
case 'function':
if (typeof cbmExist == "undefined") cbmExist = arguments[x];
else cbmNotExist = arguments[x];
break;
case 'object':
if (arguments[x] instanceof jQuery) ele = arguments[x];
else {
var obj = arguments[x];
for (y in obj) {
if (typeof obj[y] == 'function') {
if (typeof cbmExist == "undefined") cbmExist = obj[y];
else cbmNotExist = obj[y];
}
if (typeof obj[y] == 'object' && obj[y] instanceof jQuery) ele = obj[y];
if (typeof obj[y] == 'string') ele = $(obj[y]);
}
}
break;
case 'string':
ele = $(arguments[x]);
break;
}
}
}
if (typeof cbmExist == 'function') { // has at least one Callback Method
var exist = ele.length > 0 ? true : false; // strict setting of boolean
if (exist) { // Elements do exist
return ele.each(function(i) { cbmExist.apply(this, [exist, ele, i]); });
}
else if (typeof cbmNotExist == 'function') {
cbmNotExist.apply(ele, [exist, ele]);
return ele;
}
else {
if (ele.length <= 1) return ele.length > 0 ? true : false;
else return ele.length;
}
}
else { // has NO callback method, thus return if exist or not based on element existant length
if (ele.length <= 1) return ele.length > 0 ? true : false; // strict return of boolean
else return ele.length; // return actual length for how many of this element exist
}
return false; // only hits if something errored!
}
});
$.fn.extend({
exist: function() {
var args = [$(this)];
if (arguments.length) for (x in arguments) args.push(arguments[x]);
return $.exist.apply($, args);
}
});
}
})(jQuery);
you could try this, 你可以试试看
function checkSelects() {
var contains = false;
var temp = $("#selects").children();
$(temp).each(function () {
if ($(this).attr("name") == "folder3")
contains = true;
});
if (!contains)
$("#selects").append('<select name="folder3"></select>');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.