更改Onclick函数vars的值 [英] Change value of Onclick function vars
问题描述
我在按钮上附加了onClick功能.我希望能够在页面加载时以及通过其他功能将功能var从创建"更改为更新".我已经尝试过下面的代码,但是没有运气.
I have the onClick function attached to a button. I want to be able to change the function var from 'create' to 'update' on the page load and through other functions. I have tried the code below but no luck.
<input type="button" name="save_customer" id="save_customer" value="Save" onClick="customer_crud('create')" />
var js = "customer_crud('read')";
// create a function from the "js" string
var newclick = new Function(js);
// clears onclick then sets click using jQuery
$("#save_customer").attr('onClick', '').click(newclick)
任何想法.
已更新
简而言之,我想更改attr onClick,就像您要更改attr名称&一样.类型.
Ok in a nutshell i want to change the attr onClick like you would change the attr name & type.
<input type="button" name="save_customer" id="save_customer" value="Save" onClick="customer_crud('create')" />
到
<input type="button" name="save_customer" id="save_customer" value="Save" onClick="customer_crud('update')" />
谢谢
推荐答案
如果可能,不要使用内联处理程序.他们没有适当的范围,也依赖于高度赞赏的eval
来完成工作.
Don't use inline handlers if possible. They don't get proper scope, and also rely on the highly-frowned upon eval
to do their work.
相反,您可以在.js
文件中执行以下操作:
Instead, in your .js
file, you can just do this:
var action = 'create';
$('#save_customer').click(function() {
customer_crud(action);
action = 'update';
});
第一次调用处理程序时,它将执行create
,然后执行update
.
The first time the handler is invoked it'll do create
, and then subsequently do an update
.
我将工作演示放在 http://jsfiddle.net/QzaXU/
这篇关于更改Onclick函数vars的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!