如何在 Jquery 选择器中使用变量?

How can i use a variable in a Jquery selector?

我怎样才能完成这项工作?变量范围包含菜单或内容。所以我需要它根据变量范围找到 ID 为 Scope_MenuScope_Content 的输入。

var scope = $(this).data('scope');       
$('#frmEditPagevar').find('input[id="Scope_ + scope"]').prop("checked", true);
$('#frmEditPagevar').find('input[id="Scope_' + scope + '"]').prop("checked", true);

你走对了,注意你的引号和双引号

您不需要使用.find(),因为id属性在文档中是唯一的,您可以使用jquery $()选择器来选择它。

$('#Scope_' + scope).prop("checked", true);

但是如果你想使用 .find() 将代码更改为底部代码

$('#frmEditPagevar').find('input[id="Scope_'+ scope +'"]').prop("checked", true);
// or
$('#frmEditPagevar').find('#Scope_'+ scope).prop("checked", true);

由于您手头已有一个 ID,因此在理想情况下,以下内容应该有效。

var scope = $(this).data('scope');       
$("#Scope_"+ scope).prop("checked", true);

你应该使用我下面的代码::

var scope = $(this).data('scope');       
$('#frmEditPagevar').find('#Scope_' + scope).prop("checked", true);