使用纯javascript实现创建表单并提交的方法
- 4598
- jQuery
- 3
- super_dodo
- 2014/02/07
在一些应用中需要点击一个按钮或者链接实现删除或者影藏等更多的功能模块.且该模块需要实现表单的方式进行提交相关的信息验证到指定的方法.下面就是使用纯javascript进行的方法示例.可模仿改造后使用.
<ul class="actions"> <li><a href="http://localhost/nodes/91/edit" rel="inspect">编辑</a></li> <li><a href="http://localhost/nodes/91/hide" onclick="javascript:hide();return false;">隐藏</a></li> <li><a href="http://localhost/nodes/91/del" onclick="javascript:del();return false;">删除</a></li> <li><a href="http://localhost/nodes/91/del" onclick="if(confirm('..')){...del的函数体};return false;">删除</a></li> </ul>
<script type="text/javascript"> //删除的函数方法--亦可压缩组合写在一行里面 function del(){ if(confirm('Are you sure you wish to delete this node?')){ var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m); var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'lhzUtuxOsuZtuI44IVH'); f.appendChild(s); f.submit(); }; return false; } //隐藏的函数方法 function hide(){ var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'put'); f.appendChild(m); var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'lhzUtuxOsuZtuI44IVH'); f.appendChild(s); f.submit(); return false; } </script>
The heart wants what the heart wants. 心随所愿。
- 阿里大鱼PHP版本的短信发送DEMO示例代码
- 大学生“谈恋爱”了,看父母们是如何反应的?
- PHP检测访问的终端是手机还是电脑(PC Mobile)
- Apache的vhost中配置默认访问入口index-test.php的方法(Yii)
- 安装wampserver2.5时 没有找到MSVCR110.dll的解决办法
- Yii2中如何修改默认控制器Controller以及默认方法Action
- Yii2中Model中rules中自定义message无效的问题原因
- [转]每天一个linux命令(3):pwd命令
- 29 张 PPT 告诉你移动互联网是如何吃掉整个世界的
- [转]每天一个linux命令(43):traceroute命令
相关阅读
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例
热门文章
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例
最新文章
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例