我该如何做这项工作?变量作用域包含菜单或内容。所以我需要它根据变量范围找到一个ID为Scope_Menu
或Scope_Content
的输入。如何在Jquery选择器中使用变量?
var scope = $(this).data('scope');
$('#frmEditPagevar').find('input[id="Scope_ + scope"]').prop("checked", true);
我该如何做这项工作?变量作用域包含菜单或内容。所以我需要它根据变量范围找到一个ID为Scope_Menu
或Scope_Content
的输入。如何在Jquery选择器中使用变量?
var scope = $(this).data('scope');
$('#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);
$('#frmEditPagevar').find('input[id="Scope_' + scope + '"]').prop("checked", true);
你在正确的轨道上,只要留意你的引号和双引号
你真的没有不需要找到:) –
感谢您的答案,它w orked。我会在7分钟内接受你的回答。 –
@sᴜʀᴇsʜᴀᴛᴛᴀ这一切都取决于他的ID是否真的是唯一的。尽管它应该是独一无二的,而另一种方法可以工作,但这并不能保证,特别是当某些模板引擎生成类和id时。 – LordNeo
既然你已经在手的ID,在一个理想的世界,下面应该工作。
var scope = $(this).data('scope');
$("#Scope_"+ scope).prop("checked", true);
您应该将代码与我下面的代码::
var scope = $(this).data('scope');
$('#frmEditPagevar').find('#Scope_' + scope).prop("checked", true);
使用'$( '#Scope_' +范围).prop( “选中”,真)'' – Mohammad
.find(”输入[id =“Scope_”+ scope +“]')' – Andreas
模板文字 - '.find(\'input [id =”Scope _ $ {scope}“] \')' - 对我更易读https:/ /developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals –