更改表单的操作属性 [英] Change form's action attribute

查看:21
本文介绍了更改表单的操作属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一定是在这里遗漏了一些东西,因为这应该非常简单,但它不起作用......

Okay, I must be missing something here because this should be extremely simple, yet it doesn't work...

我的页面上有一个 form,其 action 属性为空(action="#").页面准备好后,我想将 action 属性更改为其他内容.我的代码很简单:

I have a form on my page with an empty action attribute (action="#"). When the page is ready, I want to change the action attribute to something else. My code is very simple:

HTML:

<form action="#" method="post" id="register"></form>​

jQuery:

$("#register").attr("action", "http://www.test.com");​

即使在 测试小提琴中,它也不起作用!查看Result框架的源码,你会看到action属性的值仍然是#.

Even in a test fiddle, it doesn't work! View the source of the Result frame and you'll see the value of the action attribute is still #.

我哪里错了??

推荐答案

它确实有效,但您没有正确测试它.查看源代码将显示页面加载时源代码的样子,而不是在它被 jQuery 修改后的样子.如果你 alert() 使用 jQuery 获取动作值后,你会看到新的值:

It does work, you aren't testing it properly. Viewing source will show you what the source looked like when the page was loaded, not after it as modified by jQuery. If you alert() action value after it is fetched with jQuery, you will see the new value:

alert($("#register").attr("action"));

在您进行任何更改之前确保 DOM 已加载通常也是一个好主意,您可以使用 ready():

It's also generally a good idea to make sure that the DOM has loaded before you make any changes, for which you can use ready():

$(document).ready(function(){ /* your code */ });

查看:http://jsfiddle.net/wvYjs/2/

这篇关于更改表单的操作属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆