【问题标题】:Validating an HTML form with onClick added form fields使用 onClick 添加的表单字段验证 HTML 表单
【发布时间】:2011-05-25 17:10:05
【问题描述】:

我有一个表格,我用网上复制的一些代码拼凑而成,所以我的 HTML 和 Javascript 知识非常基础。该表单有一个按钮,单击该按钮将添加另一组相同的表单字段。我添加了一些代码,以便如果“数量和描述”字段未填写,表单将不会提交,但现在它只是不断弹出该字段未填写的警报,即使它是。这是我的脚本:

<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'>
</script><script type='text/javascript'>
 //<![CDATA[ 
 $(function(){
 $('#add').click(function() {
var p = $(this).closest('p');

    $(p).before('<p> Quantity & Description:<br><textarea name="Quantity and Description" rows="10" 

cols="60"><\/textarea><br>Fabric Source: <input type="text" name="Fabric Source"><br>Style# & Name: <input 

type="text" name="Style# & Name"><br>Fabric Width: <input type="text" name="Fabric Width"><br>Repeat Information: 

<input type="text" name="Repeat Info" size="60"><input type="hidden" name="COM Required" /> </p><br>');
return false; 
});
 });
function checkform()
 {
var x=document.forms["comform"]["Quantity and Description"].value
if (x==null || x=="")
 {
 alert("Quantity & Description must be filled out, DO NOT just put an SO#!!");
 return false;
 }
}
 //]]> 
</script>

这是我的 HTML:

 <form action="MAILTO:ayeh@janusetcie.com" method="post" enctype="text/plain" id="comform" onSubmit="return 

checkform()">
<div>Please complete this worksheet in full to avoid any delays.<br />
<br />Date: <input type="text" name="Date" /> Sales Rep: <input type="text" name="Sales Rep" /> Sales Quote/Order#: <input type="text" name="SQ/SO#" /><br />
<br />Quantity &amp; Description: <font color="red"><i>Use "(#) Cushion Name" format.</i></font><br />
<textarea name="Quantity and Description" rows="10" cols="60">
</textarea>
<br />Fabric Source: <input type="text" name="Fabric Source" /><br />Style# &amp; Name: <input type="text" name="Style# &amp; Name" /><br />Fabric Width: <input type="text" name="Fabric Width" /><br />Repeat Information: <input type="text" name="Repeat Info" size="60" /><br /><font color="red"><i>Example: 13.75" Horizontal Repeat</i></font><br />
<br /><input type="hidden" name="COM Required" />
<p><button type="button" id="add">Add COM</button></p>
</div>
<input type="submit" value="Send" /></form>

我怎样才能让它提交但仍然检查“数量和描述”字段的每次出现?

【问题讨论】:

    标签: javascript jquery html validation forms


    【解决方案1】:

    首先,我不会在您的输入名称中使用空格,因为这样您就必须处理奇怪的转义问题。请改用“QuantityAndDescription”之类的内容。

    此外,您似乎正在尝试使用相同名称的多个字段。最好的方法是在名称中添加方括号,这意味着这些值将组合为一个数组:

    <textarea name="QuantityAndDescription[]"></textarea>
    

    这也意味着代码必须获取所有文本区域,而不仅仅是第一个。我们可以使用 jQuery 来获取我们想要的元素,循环它们,并检查值。试试这个:

    function checkform()
    {
        var success = true;
    
        // Find the textareas inside id of "comform", store in jQuery object
        var $textareas = $("form#comform textarea[name='QuantityAndDescription[]']");
    
        // Loop through textareas and look for empty values
        $textareas.each(function(n, element)
        {
            // Make a new jQuery object for the textarea we're looking at
            var $textarea = $(element);
    
            // Check value (an empty string will evaluate to false)
            if( ! $textarea.val() )
            {
                success = false;
                return false; // break out of the loop, one empty field is all we need
            }
        });
    
        if(!success)
        {
            alert("Quantity & Description must be filled out, DO NOT just put an SO#!!");
            return false;
        }
    
        // Explicitly return true, to make sure the form still submits
        return true;
    }
    

    另外,纯美学的旁注:您不再需要使用 CDATA 评论黑客。这是旧 XHTML 时代的保留,以防止严格的 XML 解析器被破坏。除非您使用的是 XHTML Strict Doctype(而且您不应该),否则您绝对不需要它。

    【讨论】:

    • 谢谢!效果很好!同样的脚本也可以用于检查复选框和单行文本字段吗?
    • 好的,我一直在尝试复制您给我的部分脚本并对其进行扩充,以同样的方式检查单行文本字段,但它似乎不起作用。我在“return true;”之前的 } 之后添加了它。并且基本上复制了从“var $textareas”开始的所有内容。我唯一更改的其他内容是 var $textarea 变成了 var $textfields,“form#comform textarea”变成了“form#comform text”,var $textarea 变成了 var $textfield。我做错了什么?
    • @Alice:选择器中的“textarea”是元素类型。将其更改为“输入”,您应该没问题。 (几乎所有其他字段类型都使用 标签,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多