How to declare local var global
I am reading data in from a grid and for the life of me, cannot seem to find a way to make a local var global work. I got some help here recently and still seem to be having problems. I have posted a small portion of the code for your reference and would be grateful if someone could ncheck it and tell me where I am going wrong? I have declared var titleitem = ''; outside of the function and need to find a way to create a global var using var = act_item portion of the code. I thought that ommiting the var, this became a global var and could be used outside of the function. In this case var titleitem. This would then be used in dialog as part of the title. $('#actionform').dialog({title : titleitem});
All that is returned in console.log is (an empty string).
Thanks
<!--- function to action data -->
<script type="text/javascript">
var titleitem = '';
</script>
<script type="text/javascript">
function action(com,grid) {
if(com == 'Action') {
if( $('.trSelected').length>0 ) {
var items = $('.trSelected');
var itemlist ='';
for(i=0;i<items.length;i++){
itemlist+= items[i].id.substr(3);
}
var act_id = $("tr.trSelected td:nth-child(1) div").text();
var act_location = $("tr.trSelected td:nth-child(2) div").text();
var act_service = $("tr.trSelected td:nth-child(3) div").text();
var act_activity = $("tr.trSelected td:nth-child(4) div").text();
var act_department = $("tr.trSelected td:nth-child(5) div").text();
var act_company = $("tr.trSelected td:nth-child(6) div").text();
var act_address = $("tr.trSelected td:nth-child(7) div").text();
var act_user开发者_StackOverflow社区 = $("tr.trSelected td:nth-child(8) div").text();
act_item = $("tr.trSelected td:nth-child(9) div").text();
$titleitem = act_item;
+++++UPDATE++++++
$("#actionform").dialog({
autoOpen: false,
resizable: true,
modal: true,
title: "Input new intake"+titleitem,
width: 470,
beforeclose: function (event, ui) {
if(flag==1){
flag=0;
jAlert("You have successfully actioned\n\rBox: "+act_item+"\n\r",
'New Intake successfull');
}
$("#ACT_message").html("");
},
close: function (event, ui) {
$("#ACT_action").get(0).reset();
$("#ACT_message").html("");
}
});
Yes, this...
act_item = $("tr.trSelected td:nth-child(9) div").text();
...will be global, but I think it is better to make it explicitly global by declaring it outside your function...
var act_item;
function action(com,grid) {
...
}
...or by explicitly making it a property of window
...
function action(com,grid) {
...
window.act_item = $("tr.trSelected td:nth-child(9) div").text();
}
Although there is often a better solution than to pollute the global namespace.
Also, be aware that this...
$titleitem = act_item;
...is a different variable name from your initial one because it starts with a $
.
If your globals are not working, make sure they're declared/initialized before you're trying to use them.
(Your code doesn't show where you're doing $('#actionform').dialog({title : titleitem});
.)
You need to name the variable "titleitem" and not "$titleitem" in your "action" function.
精彩评论