此输入提交选择器与最接近的表单提交 [英] this input submit selector with closest form submit

查看:78
本文介绍了此输入提交选择器与最接近的表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下代码的问题。我不知道为什么这个选择器不起作用,如果我点击提交它不起作用。有没有人知道没有使用id或类选择器会有什么方法?

I am having a problem with the below code. I am not sure why this selector doesn't work, if I click to submit it doesn't work. Does anybody have any idea what would be proper way without using id or class selector?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
    </head>
    <body>
        <script>
        $(this).closest('form').submit(function () {
           alert(1);
           return false;
        });
        </script>
        <form>
          <input type="text" name="example">
          <input type="submit" class="searchBtn">
        </form>
    </body>
</html>

当gdoron问我想要做什么时,我把整个代码放在这里,使用Sudhir解决方案,如果有其他人需要它。所以我试图在提交时使用ajax发送输入值,但我不想使用类或id选择器:

As gdoron were asking me what I am trying to do, I am putting here the entire code, together with Sudhir solution, if anybody else need it. So I am trying to send input values with ajax on submit, but I don't want use class or id selector:

$("input[type='submit']").click(function() {
  $(this).closest('form').submit(function() {
        var values = $(this).serialize();          
        $.ajax({
          type: "POST",
          url: $(this).closest('form').attr('action'),
          data: values,
          success: function(msg){                              
              //saving done
              closeDialog(200);                            
          }           
        });     
        return false;           
  });
});


推荐答案

尝试:

$(document).ready(function() {
  $("input[type='submit']").click(function() {
    $(this).closest('form').submit(function (evt) {
           //evt.preventDefault();
           alert(1);
           return false;
    });
  });
});

这篇关于此输入提交选择器与最接近的表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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