2012-05-03 25 views
0

我在表單上有Img標籤,我想將此ID發送至aspx.cs。我使用post方法,並使用Request.Form["id"]回顧此ID,但我沒有得到aspx.cs文件的ID。如何將點擊html標籤的值(id)發送到asp.net?

代碼:

$("img").click(function() { 
var id = $(this).attr("id");// Here I am getting id How can i send this id to aspx.cs 
jq.post("WebForm1.aspx", 
      id: $(this).attr("id")); 
}); 
+0

不要過度使用jQuery。 'this.id'會給你帶來'id',它比'$(this).attr('id')'更快更快' – gdoron

回答

0
$("img").click(function() { 
     var id = $(this).attr("id");// Here I am getting id How can i send this id to aspx.cs 
     //alert(id); 
     jq.post("WebForm1.aspx", id: id);//this is enough 
    }); 
+0

它不應該是'{id:id}'嗎? – gdoron

+0

其實我看不出你在這個答案中修復了什麼?你就像他一樣使用相同的價值。 – gdoron

0

可能有this問題得到當你打開post方法覆蓋。嘗試:

$("img").click(function() { 
     var my_id = $(this).attr("id"); 
     $.post("WebForm1.aspx", { id: my_id }); 
}); 

或者,如果你有幾個參數傳遞給表單,你可以提前建立自己的對象的時候,像這樣:

$("img").click(function() { 
     var params = {id: $(this).attr("id"), href: $(this).attr("href")}; 
     $.post("WebForm1.aspx", params); 
}); 
+0

不應該是「{id:my_id}」嗎? – gdoron

+0

是的,但我正在複製並粘貼原始示例。 – Anthony

+0

修正了它。還關閉了點擊方法。 – Anthony

0
$("img").click(function() { 
     var id = this.id; // No jquery needed! 
     jq.post("WebForm1.aspx", {id: id}); 
    }); 

不要過度使用jQuery 。 this.id會帶給你更容易的id很快$(this).attr('id')