如何将动态参数传递给jquery函数
16lz
2021-01-22
I want to pass some dynamic paramters to jquery onClick event. Before, I used this HTML and JavaScript:
我想将一些动态参数传递给jquery onClick事件。之前,我使用过这个HTML和JavaScript:
<!-- HTML -->
<a id="link123" href="link1" onClick="javascript:func1(${param1},${param2});">123</a>
/* JavaScript */
func1(param1,param2) {
// do something
}
The parameters param1
and param2
come from backend code and are dynamic. How I get this is not important here.
参数param1和param2来自后端代码并且是动态的。我如何得到这一点并不重要。
After using jQuery, I have this HTML and JS:
使用jQuery后,我有这个HTML和JS:
<a href="link123">
<input type="hidden" id="param1" value="${param1}"/>
<input type="hidden" id="param2" value="${param2}"/>
$(document).ready(function() {
$("#link123").click(function() {
var param1 = $("#param1")[0].value;
var param1 = $("#param1")[0].value;
func1(param1,param2);
});
});
func1(param1,param2) {
//do something
}
function func1 is there as before.
I'm not happy with this solution (passing params as inline hidden values). What are other, better ways to pass dynamic parameters to jQuery, in situation like this?
我对这个解决方案不满意(将params作为内联隐藏值传递)。在这种情况下,将动态参数传递给jQuery的其他更好方法是什么?
3 个解决方案
#1
4
Data attributes are advised for this purpose. Use:
建议使用数据属性。使用:
<input data-param1="${param1}" />
And then:
$('input').click(function(){
$(this).attr('data-param1');
});
更多相关文章
- 使用字符串参数调用AndroidJni静态方法。
- Java开发水吧点饮品系统(顶层父类对象做接口参数)
- java返回值是list的时候获取list的参数类型
- Java反射---getGenericSuperclass和ParameterizedType参数化类型
- 根据用户的动态字段对链接列表进行排序
- 通过javascript动态显示界面控件
- 转发可变数量的参数以通过椭圆运算符接受可变数量的参数
- 准备语句SQL异常“无输入参数”
- 使用Java中的Scala:将函数作为参数传递