用Javascript中的单引号插值 [英] Interpolation with single quotes in Javascript
问题描述
我在Ruby on Rails中的.erb文件中有这个Javascript代码(点击此处查看原始文件:
I have this Javascript code inside an .erb file in Ruby on Rails (click here to see original file):
<script type="text/javascript">
hidden = 0;
if ($('#categories_terms_root2').val() == '') {
$('#categories_terms_root2').hide();
hidden = 1;
}
if ($('#categories_terms_root3').val() == '') {
$('#categories_terms_root3').hide();
hidden = 1;
}
Gm.Slnc.hide('categories_terms_root_more');
if (hidden == 1) {
Gm.Slnc.show('categories_terms_root_more');
}
function show_all_categories_terms_contents() {
$('#categories_terms_root2').show();
$('#categories_terms_root3').show();
Gm.Slnc.hide('categories_terms_root_more');
return false;
}
</script>
#categories_terms_rootX
,用于显示或在网页中发布内容时隐藏更多类别。
此脚本用于显示最大类别数,方法是单击关联到多个按钮类别,你可以在这里看到:
http://i281.photobucket.com/albums/kk205/LEANDRO351 /Gamersmafia/categories.jpg [ ^ ]
现在,我想添加更多类别,当然如果我这样做(并更改文档中的其他行)它有效:
#categories_terms_rootX
, is used to show or hide more categories when publishing a content in the webpage.
This script is used to show max number of categories, by clicking on a button called "Associate to more than one category", as you can see here:
http://i281.photobucket.com/albums/kk205/LEANDRO351/Gamersmafia/categories.jpg[^]
Now, I want to add more categories, and of course if I do it on this way (and changing other lines in the document) it works:
<script type="text/javascript">
hidden = 0;
if ($('#categories_terms_root2').val() == '') {
$('#categories_terms_root2').hide();
hidden = 1;
}
if ($('#categories_terms_root3').val() == '') {
$('#categories_terms_root3').hide();
hidden = 1;
}
if ($('#categories_terms_root4').val() == '') {
$('#categories_terms_root4').hide();
hidden = 1;
}
if ($('#categories_terms_root5').val() == '') {
$('#categories_terms_root5').hide();
hidden = 1;
}
if ($('#categories_terms_root6').val() == '') {
$('#categories_terms_root6').hide();
hidden = 1;
}
Gm.Slnc.hide('categories_terms_root_more');
if (hidden == 1) {
Gm.Slnc.show('categories_terms_root_more');
}
function show_all_categories_terms_contents() {
$('#categories_terms_root2').show();
$('#categories_terms_root3').show();
$('#categories_terms_root4').show();
$('#categories_terms_root5').show();
$('#categories_terms_root6').show();
Gm.Slnc.hide('categories_terms_root_more');
return false;
}
</script>
但是我想创建一个循环来生成它而不需要在每次需要时重写或删除所有这些行修改最大类别的数量。
我尝试了各种各样的东西,比如下一个,但没有一个适合我:
But I want to create a loop to generate it without rewriting or deleting all those lines every time I need to modify the number of maximum categories.
I tried various things, like next one, but none worked for me:
<script type="text/javascript">
hidden = 0;
// modified block
for(var i=2; i<=6; i++){
if ($("#categories_terms_root" + i)).val() == '') {
$("#categories_terms_root" + i).hide();
hidden = 1;
}
}
// end of modified block
Gm.Slnc.hide('categories_terms_root_more');
if (hidden == 1) {
Gm.Slnc.show('categories_terms_root_more');
}
// modified block
function show_all_categories_terms_contents() {
for(var i=2; i<=6; i++){
$('#categories_terms_root' + i).show();
}
// end of modified block
Gm.Slnc.hide('categories_terms_root_more');
return false;
}
</script>
请有人告诉我发生了什么事吗?
提前致谢,对不起我的英文:P
Please, could someone tell me what''s going on?
Thanks in advance, and sorry for my english :P
推荐答案
(' #categories_terms_root2')。val()== ' '){
('#categories_terms_root2').val() == '') {
(' #categories_terms_root2'跨度>)隐藏();
hidden = 1 ;
}
if (
('#categories_terms_root2').hide(); hidden = 1; } if (
(' #categories_terms_root3')。val()== ' '){
('#categories_terms_root3').val() == '') {
这篇关于用Javascript中的单引号插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!