2010-07-19 71 views
0

我在SharePoint站点上有一个表单,并希望某些字段是必填字段(日期时间选择器等)。 做一些其他领域的可见/隐藏,并添加我使用红色*为必填后场冠军(例如):jQuery/JavaScript验证控件

//Strategic Priority 
$('nobr:contains("Strategic Priority")').closest('tr').show(); 
$('nobr:contains("Strategic Priority")').html("Strategic Priority <span class=\"validate\"> *</span>"); 

//Performance Measure 
$('nobr:contains("Performance Measure")').closest('tr').hide(); 

//Start Date 
$('nobr:contains("Start Date")').closest('tr').show(); 
$('nobr:contains("Start Date")').html("Start Date <span class=\"validate\"> *</span>"); 

什么是当用户点击提交按钮来验证必填字段元素的最佳方法是什么?

的HTML看起来像:

<td class="ms-formbody" valign="top" width="400px"> 
    <!-- FieldName="Target Date" 
    FieldInternalName="DueDate" 
    FieldType="SPFieldDateTime" 
    --> 
    <span dir="none"> 
    <script language="javascript">g_strDateTimeControlIDs["SPDueDate"] = "ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDate";</script> 

    <table><tbody><tr> 
    <td class="ms-dtinput"> 
    <label for="ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDate" style="display: none;">Target Date Date</label> 
    <input name="ctl00$m$g_c6ae303a_6013_4adb_8057_63a214bcfd24$ctl00$ctl04$ctl10$ctl00$ctl00$ctl04$ctl00$ctl00$DateTimeField$DateTimeFieldDate" maxlength="45" id="ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDate" title="Target Date" class="ms-input" autopostback="0" type="text"></td> 

生成的HTML查看按钮:

<td class="ms-toolbar" nowrap="true"> 

    <table width="100%" cellpadding="0" cellspacing="0"><tbody><tr><td width="100%" align="right" nowrap="nowrap"> 
    <input name="ctl00$m$g_c6ae303a_6013_4adb_8057_63a214bcfd24$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem" value="OK" onclick='if (!PreSaveItem()) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$m$g_c6ae303a_6013_4adb_8057_63a214bcfd24$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem", "", true, "", "", false, true))' id="ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem" accesskey="O" class="ms-ButtonHeightWidth" target="_self" type="button"> 
    </td> 

我不能使用ID或名字来获取元素,并检查它是否是空的或不是。

在此先感谢。

编辑:

我试图与 如果($( “输入[标题= '开始日期']”)VAL()长度< 1。) { 警报( “测试”); }

它不会给我一个弹出如果该字段中有一些数据,但如果它是空的它会。 下一步是在字段后面添加一些文本(而不是发出警报),说“必填字段”。如何在我验证的元素之后插入jQuery/JavaScript?

回答

0

这样的事情呢?

$("input[title='Start Date']").each(function() { 
    var obj = $(this); 
    if (obj.val().length < 1) { 
    // Check if notified already, if not, add text 
    if (!obj.data('notified')) { 
     obj.after('Required Field'); 
     obj.data('notified', true); 
    } 
    } 
});