English 中文(简体)
jQuery值选择器
原标题:jQuery value selector

我已经:

<button class="Delete" value="1">Delete</button>
<button class="Delete" value="2">Delete</button>
<button class="Delete" value="3">Delete</button>

给定变量X包含一个值(在本例中为1、2或3),那么我如何隐藏X中对应值的按钮?

我想说的是:

$( button ).val(x).hide();

意思是:“值为x的按钮,隐藏”。

最佳回答
$( button[value="  + x +  "] ).hide();
问题回答

你可以编写自己的自定义选择器(我只是觉得应该有人提到它)。可能看起来像:

(function($) {
    $.extend($.expr[ : ], {
         val: function(elem, i, attr) {
             return elem.value === attr[3];
         }
    });
}(jQuery));

$( button:val(2) ).hide();

您可以在实际选择器内部执行此操作:

$( button[value="foo"] ).hide();

这需要将x连接到xpath中,但我现在想不出其他方法。

$(button[value= 1 ]).hide();

编辑:删除了@,显然不推荐使用,很像我自己:)

您甚至不必手动为按钮赋值:

var i = 0; $( .Delete ).attr( value , function() {i++;    return   +i;});

will do it fou you. Now you have your buttons values counted. But nevermind, (you don t even need values!) You can hide THIS clicked button by doing:

$(".Delete").click(function() {
$(this).html("DELETED!").delay(900).slideUp(600).hide();
});

I ve added a bit of animation. Or combine both! CIAO :)





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签