简体   繁体   English

搜索HTML表格并查找具有必需类的文本字段

[英]Search through an HTML Table and find text fields with required classes

I'm creating a validation script for a multi-step form, each group is inside a table and I want to check that the containing table has a required field inside it. 我正在为一个多步骤表单创建一个验证脚本,每个组都在一个表内,并且我想检查其中包含表的字段是否为必填字段。

I've tried to implement this as below: 我尝试实现此如下:

(where a = table id .required = class, but the classes are like class = "something required") (其中=表ID .required =类,但这些类类似于class =“必需的东西”)

function validForm(a) {
    var myVar = $('a').find('.required').val();
    alert(myVar);
}

the problem is that this code returns undefined . 问题是此代码返回undefined This is my first time using a .find function and I am having a hard time understanding how to use it. 这是我第一次使用.find函数,我很难理解如何使用它。

HTML: HTML:

<table id = "default">
<tr><td>Default</td></tr>
<tr><td>Field name</td><td><input type="text" name="first_name" maxlength="35" class="txtfield-cu1 required" title="First Name"></td></tr> <- repeat a couple of times

if a is the table id, you will need to select by $('#a') instead of $('a') . 如果a是表ID,则需要按$('#a')而不是$('a')

In jQuery selection (and CSS) '#a' selects the tag with id = 'a' , whereas a selects the <a> tag. jQuery中选择(和CSS) '#a' id为选择标记= 'a'a选择<a>标签。

Edit: if a here stands for a variable that represents the id of the table, then you can use $(a) to select it. 编辑:如果a在这里代表表示表的id一个变量,那么你可以使用$(a)将其选中。

Edit 2: jsfiddle link 编辑2: jsfiddle链接

尝试$("#a")通过ID而不是标签名称进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM