如何将javascript变量用作jquery选择器中的参数?
<script type="text/javascript">
$(function(){
$("input").click(function(){
var x = $(this).attr("name");
$("input[id=x]").hide();
});
});
</script>
<input type="text" id="bx"/><input type="button" name="bx"/>
<input type="text" id="by"/><input type="button" name="by"/>
基本上我想要做的是能够隐藏id等于被点击元素名称的元素。
答案 0 :(得分:209)
var name = this.name;
$("input[name=" + name + "]").hide();
或者你可以这样做。
var id = this.id;
$('#' + id).hide();
或者你也可以发挥作用。
$("#" + this.id).slideUp();
如果要从页面中永久删除整个元素。
$("#" + this.id).remove();
您也可以在此处使用它。
$("#" + this.id).slideUp('slow', function (){
$("#" + this.id).remove();
});
答案 1 :(得分:54)
$(`input[id="${this.name}"]`).hide();
当您使用ID时,这会表现得更好
$(`#${this.name}`).hide();
我强烈建议您通过按钮点击隐藏元素的方法更具体。我会选择使用数据属性。例如
<input id="bx" type="text">
<button type="button" data-target="#bx" data-method="hide">Hide some input</button>
然后,在您的JavaScript中
// using event delegation so no need to wrap it in .ready()
$(document).on('click', 'button[data-target]', function() {
var $this = $(this),
target = $($this.data('target')),
method = $this.data('method') || 'hide';
target[method]();
});
现在,您可以完全控制要定位的元素以及通过HTML发生的事情。例如,您可以使用data-target=".some-class"
和data-method="fadeOut"
淡出元素集合。
答案 2 :(得分:12)
$("input").click(function(){
var name = $(this).attr("name");
$('input[name="' + name + '"]').hide();
});
也适用于ID:
var id = $(this).attr("id");
$('input[id="' + id + '"]').hide();
何时,(有时)
$('input#' + id).hide();
不起作用,as it should。
你甚至可以做到这两点:
$('input[name="' + name + '"][id="' + id + '"]').hide();
答案 3 :(得分:6)
var x = $(this).attr("name");
$("#" + x).hide();
答案 4 :(得分:5)
$("#" + $(this).attr("name")).hide();
答案 5 :(得分:2)
ES6字符串模板
如果您不需要IE / EDGE支持,这是一种简单的方法
$(`input[id=${x}]`).hide();
或
$(`input[id=${$(this).attr("name")}]`).hide();
这是一个名为template string
的es6功能
(function($) {
$("input[type=button]").click(function() {
var x = $(this).attr("name");
$(`input[id=${x}]`).toggle(); //use hide instead of toggle
});
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="bx" />
<input type="button" name="bx" value="1" />
<input type="text" id="by" />
<input type="button" name="by" value="2" />
字符串连接
如果您需要IE / EDGE支持,请使用
$("#" + $(this).attr("name")).hide();
(function($) {
$("input[type=button]").click(function() {
$("#" + $(this).attr("name")).toggle(); //use hide instead of toggle
});
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="bx" />
<input type="button" name="bx" value="1" />
<input type="text" id="by" />
<input type="button" name="by" value="2" />
DOM中的选择器作为数据属性
这是我首选的方式,因为它让你的代码真的很干
// HTML
<input type="text" id="bx" />
<input type="button" data-input-sel="#bx" value="1" class="js-hide-onclick"/>
//JS
$($(this).data("input-sel")).hide();
(function($) {
$(".js-hide-onclick").click(function() {
$($(this).data("input-sel")).toggle(); //use hide instead of toggle
});
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="bx" />
<input type="button" data-input-sel="#bx" value="1" class="js-hide-onclick" />
<input type="text" id="by" />
<input type="button" data-input-sel="#by" value="2" class="js-hide-onclick" />