我在 tms web core 有一个简单的项目。 我想在运行时通过 THTMLWEBDIV 循环创建一系列按钮,并根据每个按钮的循环索引调用 Delphi 函数。 我在 Delphi 端有一个函数,它接受输入并发送 Shomessage。 我的问题是我不知道如何从模板端调用带有参数的函数
我的单位代码:
unit Unit1;
interface
uses
System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
WEBLib.Forms, WEBLib.Dialogs, Vcl.Controls, WEBLib.WebCtrls;
type
TForm1 = class(TWebForm)
WebHTMLDiv1: TWebHTMLDiv;
procedure WebFormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure myFunc(ACode: string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.myFunc(ACode: string);
begin
ShowMessage(ACode);
end;
procedure TForm1.WebFormShow(Sender: TObject);
var
i: integer;
begin
WebHTMLDiv1.HTML.Text := '';
for i := 1 to 5 do
WebHTMLDiv1.HTML.Text := WebHTMLDiv1.HTML.Text +
'<button type="button" onclick="myFunc(' + i.ToString +
')" class="btn btn-primary btn-sm">BtnNum' + i.ToString + '</button>';
end;
end.
我的 html 模板
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>TMS Web Project</title>
<style>
</style>
</head>
<body>
<div id = "htmlDiv">
</div>
</body>
</html>
在您的示例中,您可以简单地在 JavaScript 中调用如下函数:
pas.Unit1.TForm1.myFunc('1998');
那会起作用的。所以你的完整
WebFormShow
代码将是:
procedure TForm1.WebFormShow(Sender: TObject);
var
i: integer;
begin
WebHTMLDiv1.HTML.Text := '';
for i := 1 to 5 do
WebHTMLDiv1.HTML.Text := WebHTMLDiv1.HTML.Text +
'<button type="button" onclick="pas.Unit1.TForm1.myFunc(' + i.ToString +
')" class="btn btn-primary btn-sm">BtnNum' + i.ToString + '</button>';
end;