jQuery 更改表单的操作属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13169844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:22:07  来源:igfitidea点击:

Change form's action attribute

jqueryformsattributesaction

提问by daGUY

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

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

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

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

HTML:

HTML:

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

jQuery:

jQuery:

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

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

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

Where am I going wrong??

我哪里错了??

采纳答案by doublesharp

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:

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

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

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():

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

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

Check out: http://jsfiddle.net/wvYjs/2/

退房:http: //jsfiddle.net/wvYjs/2/