更改Onclick函数vars的值

编程入门 行业动态 更新时间:2024-10-27 06:30:40
本文介绍了更改Onclick函数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.

我将工作演示放在 jsfiddle/QzaXU/

更多推荐

更改Onclick函数vars的值

本文发布于:2023-10-25 22:29:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Onclick   vars

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!