隐藏< div>与选定的索引 [英] Hide <div> with selected index
问题描述
< div id =show_hidden_delegate>
>< select id =delegate_form> 设置为1. HTML代码是由我们的应用程序生成的,所以我无法知道页面上会出现多少用户,但提到< select>
总是跟随操纵< div>
。 所以我试图找到具有id delegate_form
的选择的值,检查它在 .each()
循环并显示/隐藏div。Bellow是部分工作代码。
$(document).ready(function(){
$('* [id * = delegate_form]')。each(function(index){
if($(this).val()=='1'){
$ #show_hidden_delegate)。show();
alert(Index =+ index);
} else {
$(#show_hidden_delegate)hide();
alert(Index =+ index);
}
});
});
在我的测试输入中,第一个选择的值为1,第二个为2(无法是任何不同的值)。但执行之后,首先 div
将消失。当我尝试
使用 $(#show_hidden_delegate)。eq(index).hide()
没有发生任何事情。我在哪里犯错?
FYI:第一个警报显示Index = 0,第二个Index = 1
好的,我设法找到了解决方法。问题是,我只能编辑一个XML生成的XML文件。我在XML中看到的很多东西都不会显示在页面上。这是独立于脚本,它取决于XML文件中的其他内容。
但这里是解决方法。
$(document).ready(function(){
$('* [id * = delegate_flag]')each(function(index){
if ($(this).val()=='1'){
$('* [id * = show_hidden_delegate')。eq(index).show();
// alert index =+ index;
} else {
$('* [id * = show_hidden_delegate')。eq(index).hide();
// alert + index;
}
});
$('* [id * = delegate_form]')每个(function(index){
$(this) change(function(){
if($(this).val()=='1'){
$('* [id * = show_hidden_delegate' );
// alert(Index =+ index);
} else {
$('* [id * = show_hidden_delegate'
// alert(Index =+ index);
}
});
});
});
只有当我使用正则表达式来查找div来隐藏时,才能显示/隐藏正常。我必须检查两次,因为一个页面包含组合选择值 id =delegate_form
,其他页面包含隐藏值 id =delegate_flage
而不是组合。
I'm fixing the piece of the jQuery code on the page. The basic idea of the page is to list the users and show user's delegate (in <div id="show_hidden_delegate">
if value of the user's <select id="delegate_form">
is set to 1. HTML code is generated by our application so I can't tell how many users will occur on the page. But mentioned <select>
is always followed by manipulated <div>
.
So I'm trying to find value of the select with id delegate_form
, checking it in .each()
loop and showing/hiding div. Bellow is the partially working code.
$(document).ready(function() {
$('*[id*=delegate_form]').each(function(index){
if($(this).val() == '1'){
$("#show_hidden_delegate").show();
alert("Index = " + index);
} else {
$("#show_hidden_delegate").hide();
alert("Index = " + index);
}
});
});
In my test input, value of the first select is 1, second is 2 (there can't be any different values). But after the execution first div
will disappear. When I tried to
use $("#show_hidden_delegate").eq(index).hide()
nothing happened. Where do I make mistake?
FYI: First alert shows "Index = 0", second "Index = 1"
Ok I've managed to find the workaround. Problem is, that I'm able to edit only an XML file from which is the HTML generated. And lots of things I see in XML won't be displayed on page. This is independent from script, it depends on other stuff in XML file.
But here is the workaround.
$(document).ready(function() {
$('*[id*=delegate_flag]').each(function(index){
if($(this).val() == '1'){
$('*[id*=show_hidden_delegate').eq(index).show();
//alert("Index = " + index);
} else {
$('*[id*=show_hidden_delegate').eq(index).hide();
//alert("Index = " + index);
}
});
$('*[id*=delegate_form]').each(function(index){
$(this).change(function(){
if($(this).val() == '1'){
$('*[id*=show_hidden_delegate').eq(index).show();
//alert("Index = " + index);
} else {
$('*[id*=show_hidden_delegate').eq(index).hide();
//alert("Index = " + index);
}
});
});
});
Only when I've used regex to find div to hide, I was able to show/hide it properly. I have to check it twice because one page contains combo to select value id="delegate_form"
and other pages contain hidden value id="delegate_flage"
and no combo.
这篇关于隐藏< div>与选定的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!