JQUERY:查找表单元素
BEFORE CHAOS
我曾经
头部
<script type="text/javascript">$(function() { $("form.openid:eq(0)").openid(); });</script>
在jquery.openid.js 的
var direct = function () {
var $li = $(this);
$this.unbind('submit').submit(function() {
$id.val($this.find("li.highlight span").text());
});
$this.submit();
return false;
};
和主体
<form class="openid" method="post" action="/Login.xhtml?ReturnUrl=">
<div>
<ul class="providers">
<li class="direct" title="Google"> <img src="images/googleW.png" alt="icon" />
</ul>
</div>
</form>
部分有这个,它的作用就像一个魅力。
混乱之后
现在,由于我想将以上所有内容添加为 ASP.NET 页面中的内容,因此我
在主体部分的 MasterPage 的头部部分
<script type="text/javascript">$(function () { $("#test.openidd").openid(); });</script>
进行了适当的更改,我添加了带有 < code>id=test 且类等于 openidd
。
<div id ="test" class="openidd">
<ul class="providers">
<li class="direct" title="Google"> <img src="images/google.png" />
</ul>
</div>
那么我应该对 jquery.openid.js 进行哪些更改才能使其正常工作?
我的意思是在第一种情况下 $this
指的是 form.openid:eq(0)
在第二种情况下,$this
引用#text.openidd
并且表单永远不会提交。
我猜想 JQUERY 中存在类似 $this.FindParentForm
的东西,但我不知道!
请让您的答案尽可能简单,因为我是新手,
提前谢谢您。
更新 从到目前为止我看到的答案来看,我猜或者我还没有明确我的问题,或者我是新手。我添加了 div 标签作为元素的附件。该表单包含 jquery 混淆的其他
等。我不喜欢那样!这就是为什么在我的帖子中我相信这项工作必须在 jquery.openid.js
文件中完成...类似$this.GETParentFORM.unbind('submit').submit(function() {
$id.val($this.GETParentFORM.find("li.highlight span").text());
});
$this.GETParentFORM.submit();
BEFORE CHAOS
I used to have this one...
at the head part
<script type="text/javascript">$(function() { $("form.openid:eq(0)").openid(); });</script>
in the jquery.openid.js
var direct = function () {
var $li = $(this);
$this.unbind('submit').submit(function() {
$id.val($this.find("li.highlight span").text());
});
$this.submit();
return false;
};
and in the body part
<form class="openid" method="post" action="/Login.xhtml?ReturnUrl=">
<div>
<ul class="providers">
<li class="direct" title="Google"> <img src="images/googleW.png" alt="icon" />
</ul>
</div>
</form>
which works like a charm.
AFTER CHAOS
Now since i would like to add the all above as content in an ASP.NET page i made the appropriate changes
at the head part in The MasterPage
<script type="text/javascript">$(function () { $("#test.openidd").openid(); });</script>
in the body part, i added the div with id=test
and class equal to openidd
.
<div id ="test" class="openidd">
<ul class="providers">
<li class="direct" title="Google"> <img src="images/google.png" />
</ul>
</div>
So what changes should i make to the jquery.openid.js
to make it work?
I mean in the first case the $this
refers to form.openid:eq(0)
In the second case the $this
refers to #text.openidd
and the form is never submitted.
I guess something like this one $this.FindParentForm
exists in JQUERY, but i have no clue!
Please keep your answers as simple as possible, since i am a newbie
Thank you in advance.
UPDATE
From the answers i have seen so far, i guess or i have not set my question clear or i am soooo newbie. I added the div tag as enclosure for the elements. The form contains other <li>
's etc that jquery messes with. And i would not like that! That's why in my post i believe that the work has to be done in the jquery.openid.js
file... something like
$this.GETParentFORM.unbind('submit').submit(function() {
$id.val($this.GETParentFORM.find("li.highlight span").text());
});
$this.GETParentFORM.submit();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在使用 jQuery 的 ASP.NET 中需要注意的一件事是,任何具有
runat="server"
的控件都将具有基于原始 ID 自动生成的 ID。只是说,特别是如果您计划通过 jQuery 上的 ID 进行选择。话虽如此,据推测,任何 ASP.NET 页面中都只有一种表单(位于 MasterPage 上的表单)。因此,要选择它,您实际上要做的就是走
,但如果您想走自己的路线,我会使用
One thing to look out for in ASP.NET with jQuery is that any control that has
runat="server"
will have an auto-generated ID based on the original ID. Just saying, especially if you're planning to select via IDs on your jQuery.Having said that, supposedly, there's only one form in any ASP.NET page (the one located on your MasterPage). So to select that, all you actually have to do is go
but if you want to go your route, I'd use
代码
示例
http://jsfiddle.net/ngdcs/
CODE
EXAMPLE
http://jsfiddle.net/ngdcs/
您可能可以将其更改为:
虽然我认为最好为您的表单提供一个 id,例如:
然后将 javascript 代码更改为:
You could probably change it to:
Although I think it would be better to give your form an id like:
and then change the javascript code to:
也许试试这个?
Maybe try this?
IE
IE
由于ASP.NET修改了form的id,您仍然可以通过使用closest来获取form元素。在此代码中,您将获得 test.openidd 最接近的名为 form 的元素,它就是表单本身。
Since ASP.NET modify the form's id, you can still get the form element by using closest. In this code, you get the test.openidd's closest elemet called form which would be the form itself.