简体   繁体   中英

Passing Parameters - Dynamic HTML with JavaScript Function

I have a javascript function that dynamically builds a table. I have a function that gets called once a button inside of that table is clicked.

var bankButtonRefresh = '<a href="javascript:void(0)" onClick="refreshBankDataFunction('+parentId+')" 
class="btn-sm btn-primary permissionDeny pull-right" role="button">Refresh</a>';
row +='<td style="position:relative">' + bankButtonRefresh2 + '</td>';

When I printed out the value of bankButtonRefresh2 the value is:

<a href="javascript:void(0)" onClick="refreshBankDataFunction(a34c0000000jiecccA)" 
class="btn-sm btn-primary permissionDeny pull-right" role="button">Refresh</a>

So I can see that the parentId is passing incorrectly.

I am not trying to do anything fancy, I am just trying to get the Id passed to the function but I am getting no response from the button when I click on it.

function refreshBankDataFunction(obj){ //obj is the parentId
      alert('got into the function: ' + obj);
}

Am I not passing the parentId correctly?

Try this:

var bankButtonRefresh = '<a href="javascript:void(0)" onClick="refreshBankDataFunction({param:'+parentId+'})" class="btn-sm btn-primary permissionDeny pull-right" role="button">Refresh</a>';


function refreshBankDataFunction(obj){ //obj is the parentId
      alert('got into the function: ' + obj.data.param);
}

在构建链接的位置添加一组额外的包装单引号,例如: onClick="refreshBankDataFunction(''+parentId+'')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM