HTMLInputElement没有方法'parent' [英] HTMLInputElement has no method 'parent'

查看:78
本文介绍了HTMLInputElement没有方法'parent'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有两个非常相似的表单。

I have 2 forms on my page which are very similar.

我在jquery中进行简单验证,在使用ajax之前检查每个字段是否有值将数据发送到使用数据的php处理程序。

I'm doing simple validation in jquery to check that each field has a value before using ajax to send the data on to the php handler that uses the data.

由于表单之间的相似性,我无法知道提交的表单,所以我以为我会尝试区分表单容器div上的id

Because of the similarities between the forms, I'm not able to know which form was submitted, so I thought I'd try differentiating by an id on the form container div

<div name="contactform2" class="form">
    <div width="100%" class="con2">

        <div class="lable">
            <label for="first_name">First Name *</label>
        </div>
        <input  type="text" name="first_name" class="span4">

        <div class="lable">
            <label for="email">Email Address *</label>
        </div>
        <input  type="text" name="email" class="span4">

        <div class="lable">
            <label for="telephone">Contact Number *</label>
        </div>
        <input  type="text" name="telephone" class="span4">

        <div class="lable">
            <label for="comments">Message *</label>
        </div>
        <textarea  name="comments" rows="8" class="span4"></textarea>

        <input type="submit" value="Submit" class="btn btn-success">
    </div>
</div>

表格之间的唯一区别是,一个人有一个< ;在其中选择> ,而另一个则没有。

The only difference between the forms, really, is that one has a <select> in it and the other doesn't.

我的jquery看起来像这样:

My jquery looks like this:

$('input[type=submit]').click(function() {
    var parentname = this.parent('div').attr('name');
    console.log('Form used: ' + parentname);

    var fname = 0,
        email = 0,
        tel = 0;
        comm = 0;

    // Validate data
});

我想也许我的选择器错了,因为我通常选择id / class / tagname所以我我甚至不确定它是否有效。

I think maybe I got my selector wrong here as I normally select by id/class/tagname so I'm not even sure it's valid.

有人可以建议替代方案吗?

Can anyone suggest an alternative?

推荐答案

.parent() 一个jQuery函数,你忘了在 $()中包装这个

var parentname = $(this).parent('div').attr('name');
console.log('Form used: ' + parentname);

你应该看一下 .closest()而不是。

这篇关于HTMLInputElement没有方法'parent'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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