如何传递jQuery onClick中的多个参数
$(document).ready(function () {
var from='2022-02-21';
var to='2022-02-25';
var category ='rt';
var btndiv = '<div class="col-12 text-right">\
<input type="button" value="Generate Invoice" id="btngenerateinvoice" onclick="generateinvoice('+ from + ',' + to + ',' + category + ')" class="btn btn-blue btn-sm" /></div>';
$(".btndiv").append(btndiv);
});
function generateinvoice(ff, tt, cc) {
alert("helo");
var f = ff;
var t = tt;
var c = cc
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row btndiv">
</div>
我在OnClick生成的三参数上通过动态函数,但生成这些功能未称为
$(document).ready(function () {
var from='2022-02-21';
var to='2022-02-25';
var category ='rt';
var btndiv = '<div class="col-12 text-right">\
<input type="button" value="Generate Invoice" id="btngenerateinvoice" onclick="generateinvoice('+ from + ',' + to + ',' + category + ')" class="btn btn-blue btn-sm" /></div>';
$(".btndiv").append(btndiv);
});
function generateinvoice(ff, tt, cc) {
alert("helo");
var f = ff;
var t = tt;
var c = cc
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row btndiv">
</div>
I pass three-parameter on onclick generateinvoice function dynamically but generateinvoice these function not called
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
onclick="generateinvoice
中的值在渲染时,代码认为它是变量,并且这些值尚未定义。您可以在前面添加\'
在每个变量之后,您可以执行
onclick="generateinvoice(\''+ from + '\',\'' + to + '\',\'' +category + '\')"
没有
\'
呈现的代码可能类似于generateinvoice(from,to,category)
,但是当您添加这些代码时,代码会将变量呈现为 string,s 所以它看起来像generateinvoice('from','to','category')
演示
The problem is that the values inside
onclick="generateinvoice
when they are rendered, the code thinks it's variables and those have not been defined. You can add\'
in front and after each variable.so you can do
onclick="generateinvoice(\''+ from + '\',\'' + to + '\',\'' + category + '\')"
Without the
\'
the rendered code could look likegenerateinvoice(from,to,category)
, but when you add those the code will render the variables as string,s so it looks likegenerateinvoice('from','to','category')
Demo