如何在jQuery选择器中使用JavaScript变量? [英] How to use JavaScript variables in jQuery selectors?

查看:116
本文介绍了如何在jQuery选择器中使用JavaScript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JavaScript变量作为jQuery选择器中的参数?

How do I use JavaScript variables as a parameter in a jQuery selector?

<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的元素,该元素等于要单击的元素的名称.

Basically what I want to do is to be able to hide the element which has an id that is equal to the name of the element that is being clicked.

推荐答案

var name = this.name;
$("input[name=" + name + "]").hide();

或者您可以做这样的事情.

OR you can do something like this.

var id = this.id;
$('#' + id).hide();

或者您也可以发挥作用.

OR you can give some effect also.

$("#" + this.id).slideUp();

如果要从页面上永久删除整个元素.

If you want to remove the entire element permanently form the page.

$("#" + this.id).remove();

您也可以在此使用它.

$("#" + this.id).slideUp('slow', function (){
    $("#" + this.id).remove();
});

这篇关于如何在jQuery选择器中使用JavaScript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆