使用jQuery检查SharePoint字段是否存在

时间:2014-07-31 08:08:35

标签: jquery sharepoint

我想使用一个脚本来检查表单中是否存在名为Delivery Terms的字段,如果这是真的,则应该执行一个函数。

为什么以下不起作用?

if ($('nobr:contains("Delivery Terms")').length > 0) {
    $jq(document).ready(function () {
        {
            $jq().SPServices.SPCascadeDropdowns({
                relationshipList: "Machine",
                relationshipListParentColumn: "Costing_x0020_Group_x003a__x0020",
                relationshipListChildColumn: "Title",
                parentColumn: "Manufacturing Unit",
                childColumn: "Machine",
                debug: true
            });
        }
    }
});

以下是我使用的完整代码:

<script type="text/javascript">

//Fixes the design for dropdowns with more than 20 items 
$jq().SPServices.SPComplexToSimpleDropdown({
 columnName: "Responsible Designer/Quoter"
});
$jq().SPServices.SPComplexToSimpleDropdown({
 columnName: "Material No. 1"
});
$jq().SPServices.SPComplexToSimpleDropdown({
 columnName: "Material No. 2"
});

if ($('nobr:contains("Delivery Terms")').length > 0) {
    $jq(document).ready(function () {
        {
        $jq().SPServices.SPCascadeDropdowns({
            relationshipList:"Machine",
            relationshipListParentColumn:"Costing_x0020_Group_x003a__x0020",
            relationshipListChildColumn:"Title",
            parentColumn:"Manufacturing Unit",
            childColumn:"Machine",
            debug:true
        });
        $jq().SPServices.SPCascadeDropdowns({
            relationshipList:"Art no",
            relationshipListParentColumn:"Costing_x0020_Group_x003a__x0020",
            relationshipListChildColumn:"Title",
            parentColumn:"Manufacturing Unit",
            childColumn:"Material No. 1",
            debug:true
        }); 
        $jq().SPServices.SPCascadeDropdowns({
            relationshipList:"Art no",
            relationshipListParentColumn:"Costing_x0020_Group_x003a__x0020",
            relationshipListChildColumn:"Title",
            parentColumn:"Manufacturing Unit",
            childColumn:"Material No. 2",
            debug:true
        });
        $jq().SPServices.SPCascadeDropdowns({
            relationshipList:"Approval",
            relationshipListParentColumn:"Title",
            relationshipListChildColumn:"Reason",
            parentColumn:"Issue Status",
            childColumn:"Reason to decline",
            debug:true
        });
        $jq().SPServices.SPCascadeDropdowns({
            relationshipList:"Responsible Designer",
            relationshipListParentColumn:"Costing_x0020_Group_x003a__x0020",
            relationshipListChildColumn:"Title",
            parentColumn:"Manufacturing Unit",
            childColumn:"Responsible Designer/Quoter",
            debug:true
        });

        $jq().SPServices.SPCascadeDropdowns({
            relationshipList:"Delivery Terms",
            relationshipListParentColumn:"Manufacturing_x0020_Place",
            relationshipListChildColumn:"Title",
            parentColumn:"Manufacturing Unit",
            childColumn:"Delivery Terms",
            debug:true
        });

        $jq().SPServices.SPCascadeDropdowns({
            relationshipList:"Payment Terms",
            relationshipListParentColumn:"Manufacturing_x0020_Place",
            relationshipListChildColumn:"Title",
            parentColumn:"Manufacturing Unit",
            childColumn:"Payment Terms",
            debug:true
         });
        }
    }
});

</script>

2 个答案:

答案 0 :(得分:0)

似乎&#34; $(&#39; nobr:包含(&#34;交付条款&#34;)&#39;)。长度&#34;不是你需要的。尝试其中之一:

$('nobr:contains("Delivery Terms")').val().length
$('nobr:contains("Delivery Terms")').text().length

另外,检查选择器&#34; nobr:是否包含(&#34;交付条款&#34;)&#34;找到所需的元素,可能是空的,代码不能调用。

答案 1 :(得分:0)

只需将if语句放入document.ready

即可

//Fixes the design for dropdowns with more than 20 items 
$().SPServices.SPComplexToSimpleDropdown({
    columnName: "Responsible Designer/Quoter"
});
$().SPServices.SPComplexToSimpleDropdown({
    columnName: "Material No. 1"
});
$().SPServices.SPComplexToSimpleDropdown({
    columnName: "Material No. 2"
});
$(document).ready(function () {
    if ($('nobr:contains("Delivery Terms")').length > 0) {
        $().SPServices.SPCascadeDropdowns({
            relationshipList: "Machine",
            relationshipListParentColumn: "Costing_x0020_Group_x003a__x0020",
            relationshipListChildColumn: "Title",
            parentColumn: "Manufacturing Unit",
            childColumn: "Machine",
            debug: true
        });
        $().SPServices.SPCascadeDropdowns({
            relationshipList: "Art no",
            relationshipListParentColumn: "Costing_x0020_Group_x003a__x0020",
            relationshipListChildColumn: "Title",
            parentColumn: "Manufacturing Unit",
            childColumn: "Material No. 1",
            debug: true
        });
        $().SPServices.SPCascadeDropdowns({
            relationshipList: "Art no",
            relationshipListParentColumn: "Costing_x0020_Group_x003a__x0020",
            relationshipListChildColumn: "Title",
            parentColumn: "Manufacturing Unit",
            childColumn: "Material No. 2",
            debug: true
        });
        $().SPServices.SPCascadeDropdowns({
            relationshipList: "Approval",
            relationshipListParentColumn: "Title",
            relationshipListChildColumn: "Reason",
            parentColumn: "Issue Status",
            childColumn: "Reason to decline",
            debug: true
        });
        $().SPServices.SPCascadeDropdowns({
            relationshipList: "Responsible Designer",
            relationshipListParentColumn: "Costing_x0020_Group_x003a__x0020",
            relationshipListChildColumn: "Title",
            parentColumn: "Manufacturing Unit",
            childColumn: "Responsible Designer/Quoter",
            debug: true
        });

        $().SPServices.SPCascadeDropdowns({
            relationshipList: "Delivery Terms",
            relationshipListParentColumn: "Manufacturing_x0020_Place",
            relationshipListChildColumn: "Title",
            parentColumn: "Manufacturing Unit",
            childColumn: "Delivery Terms",
            debug: true
        });

        $().SPServices.SPCascadeDropdowns({
            relationshipList: "Payment Terms",
            relationshipListParentColumn: "Manufacturing_x0020_Place",
            relationshipListChildColumn: "Title",
            parentColumn: "Manufacturing Unit",
            childColumn: "Payment Terms",
            debug: true
        });
    }
});

</script>