admin管理员组

文章数量:1392897

I use the following script function for get the row id from the grid. It was working properly. I get the ID within the click function. But if I try to get the outside of the function it will display Empty. I need the click function value for selr outside? How to do this?

var selr='';           
$(document).ready(function(){
               $("#datagrid").click(function(e) {
                 row = jQuery(e.target).closest('tr');
                 getId= row.attr("id");//for getting row number
                 selr = $("#datagrid").getCell(getId,'panyid');//getting Row ID
                 alert('alert in'+selr);
                 });
                  alert('alert out'+selr);
});

I use the following script function for get the row id from the grid. It was working properly. I get the ID within the click function. But if I try to get the outside of the function it will display Empty. I need the click function value for selr outside? How to do this?

var selr='';           
$(document).ready(function(){
               $("#datagrid").click(function(e) {
                 row = jQuery(e.target).closest('tr');
                 getId= row.attr("id");//for getting row number
                 selr = $("#datagrid").getCell(getId,'panyid');//getting Row ID
                 alert('alert in'+selr);
                 });
                  alert('alert out'+selr);
});
Share Improve this question edited Sep 4, 2012 at 5:33 Praveen Kumar Purushothaman 167k27 gold badges213 silver badges260 bronze badges asked Sep 4, 2012 at 5:31 UserUser 1,66210 gold badges41 silver badges67 bronze badges 3
  • selr will be assigned only on the trigger of click function. It will be empty for the rest of the cases. wat do you need exactly? – Vivek S Commented Sep 4, 2012 at 5:34
  • @InternalServerError i need to get the value for selr in outside of the click function – User Commented Sep 4, 2012 at 5:37
  • you can have the value only 1) if the click is called before and 2) if the getcell method returns proper value. check those two – Vivek S Commented Sep 4, 2012 at 5:51
Add a ment  | 

5 Answers 5

Reset to default 4

The thing is, the value of selr gets declared only when you initiate the click function. So check after clicking the jqGrid.

The script inside the $(document).ready(); will not work, and will show as empty because, after the document is ready, the selr wouldn't have set.

Instead of having a simple variable, assign selr as a global variable. Just replace selr with window.selr.

window.selr='';           
$(document).ready(function(){
    $("#datagrid").click(function(e) {
     row = jQuery(e.target).closest('tr');
     getId= row.attr("id");//for getting row number
     window.selr = $("#datagrid").getCell(getId,'panyid');//getting Row ID
     alert('alert in'+window.selr);
     });
     alert('alert out'+window.selr);
});

You're assigning the value inside a click handler, so it won't get set until the user actually clicks.

If you test selr and find it still equal to the value you initialise it to ('') you know the user hasn't yet clicked on the grid.

The problem is that the second alert alert('alert out'+selr); will be called before the .click function has been executed. So, therefore the value of selr is not set.

If you find a way to circumvent this, you can further assure to access a global variable by using the window object. E.g.

window.selr = null
 ...
window.selr = your value

However, usually such global variables should be avoided at all costs.

You are already assigning the variable as global.

The problem is the variable is initiated inside a function which is triggered on click of $("#datagrid") and the alert('alert out'+selr); executes upon DOM Ready Event, most probably before the click event triggers.

you can not get selr value outside click function ,
if datagrid is table id then $("#datagrid tr").eq(0) will give you the first row

本文标签: jqueryhow to assign the value for global variable in javascriptStack Overflow