如何在 jquery 选择器中连接字符串和 javascript 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5911815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:28:45  来源:igfitidea点击:

How to concatenate string and javascript variable inside jquery selector

javascriptjquery

提问by user225269

How do I concatenate a string with a javascript variable inside a jquery selector? If I execute the code below. All the other form elements gets hidden.

如何将字符串与 jquery 选择器中的 javascript 变量连接起来?如果我执行下面的代码。所有其他表单元素都被隐藏。

var inputatr = $('input[type=text]').attr(); //button
$('input[value=string ' +  inputatr +']').hide(); //textbox

Update

更新

<?php for($x=0; $x<4; $x++){ ?>

<input type="text" id="<?php echo $x; ?>" name="<?php echo $x; ?>" value="text<?php echo $x; ?>"/>
<input type="button" name="show<?php echo $x; ?>" value="string<?php echo $x; ?>"/></li>

<?php } ?>

回答by Raynos

$('input[value="string' + inputatr +'"]').hide();

$('input[value="string' + inputatr +'"]').hide();

This hides the input with the value equal to inputatr

这将隐藏值等于的输入 inputatr

回答by Jim Rubenstein

Are you trying to locate all your textboxes and hide the one with the value of 'string'? If so, the way to do that is actually something more like this:

您是否试图找到所有文本框并隐藏值为 的文本框'string'?如果是这样,这样做的方法实际上更像是这样的:

$('input[type=text]').filter('input[value=string]').hide()

Updated because of your update:

由于您的更新而更新:

$('input[type=button]').click(function(event)
{
    var strNum = $(this).val('name').replace('string', '');

    $('#' + strNum).hide();
}