如何检查表单中是否存在元素

时间:2017-01-19 15:22:16

标签: jquery

我用Google搜索了但我无法在我的代码中执行此操作。我有一个包含隐藏字段的表格,还有Text AreaText Field。我创建了一个按钮,当我单击时,添加所有隐藏的字段以形成。但我希望如果我再次单击该按钮,如果该字段已经在表单中,那么它不应该添加到表单中。我试过像

var $form = $(form);
var contains = $form.has($element).length;
var length = $form.find($element).length
if (!($form.find($element).length)) {

}

但没有一个有用。每次我点击按钮。 Fields再次添加到表单中。这是我的html和jQuery代码

<body>
    <table width="95%" class="CustomeFields" id="udfTable" border="0" cellspacing="0" cellpadding="4">
        <input name="valueContextID" type="hidden" value="715185080">
        <input name="udfCount" type="hidden" value="4">
        <input name="UDFudfID" id="UDFudfID" type="hidden" value="253604743">
        <input name="UDF253604743udfValueID" id="UDF253604743udfValueID" type="hidden" value="">
        <input name="UDF253604743udfValue" id="UDF253604743udfValue" type="hidden" value="">
        <tbody>
            <tr>
                <td width="28%" align="right" class="label" valign="top">UDF 07 Test:   </td>
                <td style="text-align:left">
                    <input name="UDF253604743" width="90%" id="UDF253604743" onchange="setTrue()" type="text" maxlength="1024" value="">
                </td>
            </tr>
            <tr>
                <input name="UDFudfID" id="UDFudfID" type="hidden" value="487684375">
                <input name="UDF487684375udfValueID" id="UDF487684375udfValueID" type="hidden" value="251358996">
                <input name="UDF487684375udfValue" id="UDF487684375udfValue" type="hidden" value="Basit &quot; # $ % &amp; ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _  { | } ~ € ‚ ƒ ˆ ‹ ‘ ’ “ ” • – —  ™ ¦ © « ® ² ³ ´ ¹ º » ¼ ½ ¾ ¿ £ ¥¢ ± ÷">
                <td width="28%" align="right" class="label" valign="top" style="text-align:right">UDF 08 Test:  </td>
                <td style="text-align:left">
                    <textarea name="UDF487684375" id="UDF487684375" onchange="setTrue()" rows="4" cols="70">Basit " # $ % &amp; ' ( ) * + , - . / : ; &lt; = &gt; ? @ [ \ ] ^ _  { | } ~ € ‚ ƒ ˆ ‹ ‘ ’ “ ” • – —  ™ ¦ © « ® ² ³ ´ ¹ º » ¼ ½ ¾ ¿ £ ¥¢ ± ÷</textarea>
                </td>
            </tr>
            <input name="UDFudfID" id="UDFudfID" type="hidden" value="1874287456">
            <input name="UDF1874287456udfValueID" id="UDF1874287456udfValueID" type="hidden" value="">
            <input name="UDF1874287456udfValue" id="UDF1874287456udfValue" type="hidden" value="">
            <tr>
                <td width="28%" align="right" class="label" valign="top">UDF 11 Test:   </td>
                <td style="text-align:left">
                    <input name="UDF1874287456" width="90%" id="UDF1874287456" onchange="setTrue()" type="text" maxlength="1024" value="">
                </td>
            </tr>
            <tr>
                <input name="UDFudfID" id="UDFudfID" type="hidden" value="1722904164">
                <input name="UDF1722904164udfValueID" id="UDF1722904164udfValueID" type="hidden" value="">
                <input name="UDF1722904164udfValue" id="UDF1722904164udfValue" type="hidden" value="">
                <td width="28%" align="right" class="label" valign="top" style="text-align:right">UDF 12 Test:  </td>
                <td style="text-align:left">
                    <textarea name="UDF1722904164" id="UDF1722904164" onchange="setTrue()" rows="4" cols="70"></textarea>
                </td>
            </tr>
        </tbody>
    </table>

    <form name="formHidden" method=post>

    </form>

    <button type="button" onclick="addUdfTableFieldsToForm(document.formHidden)">addUdfTableFieldsToForm</button>

    <script language="javascript" type="text/javascript">

        function addHiddenField(hiddenFieldName, hiddenFieldValue, hiddenFieldId, form) {
            $('<input>').attr({
                type: 'hidden',
                id: hiddenFieldId,
                name: hiddenFieldName,
                value : hiddenFieldValue
           }).appendTo(form)
        }

        function addElementToForm($element, form) {

            if ($element.length) {
                var $form = $(form);
                var contains = $form.has($element).length; // not working
                var length = $form.find($element).length  // not working
                if (!($form.find($element).length)) {   // not working
                    var elementId = $element.attr('id');
                    var elementName = $element.attr('name');
                    var elementValue = $element.val();
                    addHiddenField(elementName, elementValue, elementId, form);
                }
            }
        }

        function addUdfTableFieldsToForm(form) {

            var $table = $('#udfTable');
            if ($table.length) {

                var valueContextId = null;
                var udfCount = null;

                // find by multiple Attribute. Same as $("input[type='hidden'][name='ProductCode']")
                var $valueContextId = $table.find('input:hidden[name=valueContextID]');
                var $udfCount = $table.find('input:hidden[name=udfCount]');

                if ($valueContextId.length) {
                    valueContextId = $valueContextId.attr('value');
                    addHiddenField("valueContextID", valueContextId, "", form)
                }

                if ($udfCount.length) {
                    udfCount = $udfCount.attr('value');
                    addHiddenField("udfCount", udfCount, "", form)
                }

                var rows = $table.find('tbody > tr').each(function(){
                    var $row = $(this);
                    var $cell = $row.children('td').eq(1);
                    var $textArea = $cell.find('textArea');
                    var $textField = $cell.find('input:text');
                    if ($textArea.length) {
                        var textAreaId = $textArea.attr('id');
                        var textAreaIdNumeric = textAreaId.replace(/\D+/g, '');
                        var $UDFudfID = $row.find('input:hidden[value='+ textAreaIdNumeric +']');
                        var $udfValueID = $row.find('input:hidden[id=UDF'+ textAreaIdNumeric + 'udfValueID]');
                        var $udfValue = $row.find('input:hidden[id=UDF'+ textAreaIdNumeric + 'udfValue]');

                        addElementToForm($UDFudfID, form);
                        addElementToForm($udfValueID, form);
                        addElementToForm($udfValue, form);
                        addElementToForm($textArea, form);  
                    }

                    if ($textField.length) {

                        var textFieldId = $textField.attr('id');
                        var textFieldIdNumeric = textFieldId.replace(/\D+/g, '');
                        var $UDFudfID = $table.find('input:hidden[value='+ textFieldIdNumeric +']');
                        var $udfValueID = $table.find('input:hidden[id=UDF'+ textFieldIdNumeric + 'udfValueID]');
                        var $udfValue = $table.find('input:hidden[id=UDF'+ textFieldIdNumeric + 'udfValue]');

                        addElementToForm($UDFudfID, form);
                        addElementToForm($udfValueID, form);
                        addElementToForm($udfValue, form);
                        addElementToForm($textField, form);
                    }
                }); 
            }
        }

    </script>
</body>

我该如何检查?感谢

0 个答案:

没有答案