JQUERY:查找表单元素

发布于 2024-10-26 00:22:49 字数 1996 浏览 1 评论 0原文

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 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(6

    萌无敌 2024-11-02 00:22:49

    在使用 jQuery 的 ASP.NET 中需要注意的一件事是,任何具有 runat="server" 的控件都将具有基于原始 ID 自动生成的 ID。只是说,特别是如果您计划通过 jQuery 上的 ID 进行选择。

    话虽如此,据推测,任何 ASP.NET 页面中都只有一种表单(位于 MasterPage 上的表单)。因此,要选择它,您实际上要做的就是走

    $('form')
    

    ,但如果您想走自己的路线,我会使用

    $('#test.openidd').closest('form')
    

    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

    $('form')
    

    but if you want to go your route, I'd use

    $('#test.openidd').closest('form')
    
    叹沉浮 2024-11-02 00:22:49

    代码

    $(this).parents('form#myId')...
    

    示例

    http://jsfiddle.net/ngdcs/

    CODE

    $(this).parents('form#myId')...
    

    EXAMPLE

    http://jsfiddle.net/ngdcs/

    夜血缘 2024-11-02 00:22:49

    您可能可以将其更改为:

    <script type="text/javascript">$(function () { $("#test.openidd").parents('form').openid(); });</script>
    

    虽然我认为最好为您的表单提供一个 id,例如:

    <form id="submitForm5" ...>
    

    然后将 javascript 代码更改为:

    <script type="text/javascript">$(function () { $("#submitForm5").openid(); });</script>
    

    You could probably change it to:

    <script type="text/javascript">$(function () { $("#test.openidd").parents('form').openid(); });</script>
    

    Although I think it would be better to give your form an id like:

    <form id="submitForm5" ...>
    

    and then change the javascript code to:

    <script type="text/javascript">$(function () { $("#submitForm5").openid(); });</script>
    
    鹿港巷口少年归 2024-11-02 00:22:49

    也许试试这个?

    $("#test.openidd").parent().openid();
    

    Maybe try this?

    $("#test.openidd").parent().openid();
    
    羅雙樹 2024-11-02 00:22:49
    $('form').find('li.highlight span').text();
    

    IE

    $('form').unbind('submit').submit(function() {
       $id.val($('form').find("li.highlight span").text());
       });
    $('form').submit();
    
    $('form').find('li.highlight span').text();
    

    IE

    $('form').unbind('submit').submit(function() {
       $id.val($('form').find("li.highlight span").text());
       });
    $('form').submit();
    
    对你的占有欲 2024-11-02 00:22:49

    由于ASP.NET修改了form的id,您仍然可以通过使用closest来获取form元素。在此代码中,您将获得 test.openidd 最接近的名为 form 的元素,它就是表单本身。

    $('#test.openidd').closest('form').unbind('submit').submit(function() {
       $id.val($('#test.openidd').closest('form').find("li.highlight span").text());
       });
    $('#test.openidd').closest('form').submit();
    

    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.

    $('#test.openidd').closest('form').unbind('submit').submit(function() {
       $id.val($('#test.openidd').closest('form').find("li.highlight span").text());
       });
    $('#test.openidd').closest('form').submit();
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文