2010-05-06 66 views
1
function Psend() 
{ 

    new Ajax.Request('Handler.ashx', 
     { 
      method: 'get', 
      onSuccess: function(transport) { 
       var response = transport.responseText || "no response text"; 
       //alert("Success! \n\n" + response); 
       var obj = response.evalJSON(true); 

       for (i = 0; i < 4; i++) { 

        DeCheBX = $('MyDiv').insert(new Element('input', { 'type': 'checkbox', 'id': "img" + obj[i].Nam, 'value': obj[i].IM, 'onClick': 'SayHi(this,i)' })); 
        DeImg = $('MyDiv').insert(new Element('img', { 'id': "img" + obj[i].Nam, 'src': obj[i].IM, 'style': 'display = inline', 'onClick': 'Say(this)' })); 
        document.body.appendChild(DeCheBX); 
        document.body.appendChild(DeImg); 

       } 

      }, 
      onFailure: function() { alert('Something went wrong...') } 
     }); 


     SayHi = function(x,i) { 

      if ($(x).checked == true) { 

       //    $('id').hide(); 

       **$('img'+i).style.visibility = "hidden";**// doesnt work 
      } 



     }; 

Handler.ashx如何隱藏圖像?我能怎麼做?

public class Handler : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 

     string[] Img = new string[5] { "http://farm4.static.flickr.com/3210/3033577103_f80cb2e399_t.jpg", "http://farm1.static.flickr.com/76/184936863_dceeaa048c_t.jpg", "http://farm4.static.flickr.com/3133/2630880079_9035711f2f_t.jpg", "http://farm4.static.flickr.com/3064/2395929114_a4d69a22c6_t.jpg", "http://farm3.static.flickr.com/2195/2214604053_1de19931cf_t.jpg" }; 
     int[] Name = new int[5] { 1, 2, 3, 4, 5 }; 
     StringBuilder output = new StringBuilder(); 
     // output.Append("\"Images\":\" "); 
     output.Append("["); 
     for (int i = 0; i < 5; i++) 
     { 
      output.Append("{"); 
      output.Append("\"Nam\":\"" + Name[i].ToString() + "\","); 
      output.Append("\"IM\":\"" + Img[i] + "\" "); 
      if (i != 4) 
      { 
       output.Append("},"); 
      } 
     } 

     output.Append("}]"); 
     context.Response.Write(output); 



    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

} 

輸出

<input id="Button1" value="button" onclick=" Psend()" type="button"> 
<div id="MyDiv"> 

<input onclick="SayHi(this)" value="http://farm4.static.flickr.com/3210/3033577103_f80cb2e399_t.jpg" id="img1" type="checkbox"><img onclick="Say(this)" style="" src="http://farm4.static.flickr.com/3210/3033577103_f80cb2e399_t.jpg" id="img1"><input onclick="SayHi(this)" value="http://farm1.static.flickr.com/76/184936863_dceeaa048c_t.jpg" id="img2" type="checkbox"><img onclick="Say(this)" style="" src="http://farm1.static.flickr.com/76/184936863_dceeaa048c_t.jpg" id="img2"><input onclick="SayHi(this)" value="http://farm4.static.flickr.com/3133/2630880079_9035711f2f_t.jpg" id="img3" type="checkbox"><img onclick="Say(this)" style="" src="http://farm4.static.flickr.com/3133/2630880079_9035711f2f_t.jpg" id="img3"><input onclick="SayHi(this)" value="http://farm4.static.flickr.com/3064/2395929114_a4d69a22c6_t.jpg" id="img4" type="checkbox"><img onclick="Say(this)" style="" src="http://farm4.static.flickr.com/3064/2395929114_a4d69a22c6_t.jpg" id="img4"> 
</div> 
+0

這是如何觸發的?另外,你有幾個變量你使用的變量在他們之前沒有,他們在別處定義?沒有足夠的信息。你能清理代碼的格式並顯示一些相關的html嗎? – artlung 2010-05-06 17:33:13

+0

所有變量定義爲 – Vicky 2010-05-07 05:27:28

+0

它創建動態頁面...就像顯示圖像和複選框。我得到所有這些。我希望這將有助於你u – Vicky 2010-05-07 05:33:14

回答

1
$('img'+i) 

i是未定義的。

更新:您正在將元素ID設置爲"img" + obj[i].Nam

+0

我很抱歉,我忘了更新我的program.still是不工作 – Vicky 2010-05-06 07:09:16

+0

所有變量定義... – Vicky 2010-05-07 05:27:09