简体   繁体   English

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

[英]How to concatenate string and javascript variable inside jquery selector

How do I concatenate a string with a javascript variable inside a jquery selector? 如何在jquery选择器内连接字符串和javascript变量? If I execute the code below. 如果我执行下面的代码。 All the other form elements gets hidden. 所有其他表单元素都被隐藏。

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 } ?>

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

这将使用等于inputatr的值隐藏输入

Are you trying to locate all your textboxes and hide the one with the value of 'string' ? 您是否尝试找到所有文本框并隐藏值为'string'文本框? If so, the way to do that is actually something more like this: 如果是这样,那么这样做的方式实际上是这样的:

$('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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM