2010-07-19 73 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); 
    } 
    } 
});