2010-04-28 77 views
0
var value; 
    $("#Grid1").click(function(e) { 
      var row = jQuery(e.target).parent(); 
      value= row.attr("id"); 
     }); 
     var onrowclick = function() { 
     ("#Grid1").click($("#showgrid").load('/Names/Friends/satish/' + value)); 
    }; 

我試圖發送URL上的價值.. 當我給這樣的,我沒有得到的輸出結果Jquery的行ID ... 我在做正確此?如何獲得單一的點擊

是我在同一個網格上處理點擊事件兩次的問題?

+1

你在這裏失去了一些代碼?您的名爲onrowclick *的函數爲網格指定了單擊處理函數,但未在提供的代碼中調用它。 – 2010-04-28 20:17:46

+0

你對'價值'有什麼看法? – 2010-04-28 20:20:14

+0

值I我會得到該行的行值... – kumar 2010-04-28 20:29:57

回答

1

更新:在性反應,以您的評論:

如果你想分配click事件之外的變量,你可以做到這一點

現在

$(function() { 
    // declare the variable.. this one will hold the parent id 
    var my_value; 
    // now the click event... 
    $("#Grid1").click(function(e) { 
      e.preventDefault(); 
      // my_value now is setted outside... 
      my_value = $(this).parent().attr("id"); 
     }); 

    // now for the example we observe the click event you can use it in other way! 

    $('#Grid1').bind('click' , function() { 
     // now as you can see we are using my_value that come from outside! 
    $('#showgrid').load('/Names/Friends/satish/' + my_value); 

    }); 

}); 

附加註釋i不知道#Grid1是否在#showgrid之內,但如果是這種情況,則需要使用live()方法!

這樣的:

$("#Grid1").live('click' , function(e) { 
// do something here as above...! 
}); 

假設:

<div id="this_is_parent_of_Grid1"> 
<a id="Grid1" href="">click</a> 
</div> 

OR如果使用

<table><tr> 
<td id="this_is_parent_of_Grid1"><a id="Grid1">click</a></td> 
</tr></table> 
+0

我可以寫這個來獲取var value =「0」(row); var value =「0」(row); value = row.attr(「 ID「); }); 我需要獲得值的值? 謝謝 – kumar 2010-04-28 20:42:50

+0

通過使用** e.preventDefault(); **讓您單擊兩次相同的網格! – 2010-04-28 21:05:14

0

你需要做什麼?

var value; 

$("#rowid").click(function() { 
    value = $(this).value(); 
}); 

,或者你可以嘗試這樣的事:

var value; 

$("#rowid").click(function() { 
    value = $(this).value(); 
    $("#showgrid").html($.load("/Names/Friends/Satish/" + value)); 
}); 
+0

謝謝, 但我想這個變種onrowclick ..變量BEC我在服務器端Rowclick EVNT assiging這... 謝謝 – kumar 2010-04-28 20:29:05

+0

使用這種方法(基於ID),您將需要手動分配一個點擊事件處理到每一行。 kumar似乎將處理程序分配給行的容器,並依靠冒泡來激發代碼。如果您有很多行,或者經常添加和刪除行,那麼這是一種很好且有效的方法。 – user113716 2010-04-28 20:30:19

2

硬盤沒有看到HTML的說法,但我認爲你正在試圖做這樣的事情:

// Initialize variables outside of the handler 
var row; 
var value; 

$("#Grid1").click(function(e) { 
    // Assign values to variables when event handler fires 
    row = jQuery(e.target).closest('tr'); 
    value= row.attr("id"); 
    $("#showgrid").load('/Names/Friends/satish/' + value); 
}); 

// Now you can access the local variables 

function someFunction() { 
    alert(value); 
    alert(row.get(0)); 
} 

我因爲我不知道您的HTML結構,所以將parent()更改爲closest()


編輯:

爲清楚起見,我假設#Grid1是某種容器(table也許),以及您在tr的內容點擊的東西。而#showgrid是顯示附加信息的容器外部的獨立組件。

編輯:

我會從你的意見,你需要從處理程序外部訪問的行元素和/或ID承擔。我已經更新了示例以反映如何完成此操作。讓我知道這是不是你的意思。

+0

是你的完美..同時顯示ShowGrid我將第一個網格行id值傳遞給Actionresult方法..以獲得該特定用戶.. 謝謝 – kumar 2010-04-28 20:33:55

+0

不客氣。 – user113716 2010-04-28 20:38:51

+0

我可以寫這個來獲取var var value = $(「#Grid1」)的行值。click(function(e){var row = jQuery(e.target).parent(); value = row.attr (「ID」); });我需要獲得值的行值?謝謝 – kumar 2010-04-28 20:49:47

0

爲什麼不使用以下內容?

$("#Grid1").click(function(e) { 
     var value= $(e.target).parent().attr("id"); 
     $("#Grid1").load('/Names/Friends/satish/' + value); 
    }); 
}; 

此代碼仍然很脆弱,但可能會做更多你想要的。