且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

隐藏< div>与选定的索引

更新时间:2023-02-06 17:39:41

好的,我设法找到了解决方法。问题是,我只能编辑一个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.