jQuery/JavaScript验证控件
我在sharepoint网站上有一个表单,希望某些字段是必填字段日期时间选择器等。。 要使一些其他字段可见/隐藏,并在所需字段标题后添加一个红色*,我使用以下示例:jQuery/JavaScript验证控件,javascript,jquery,Javascript,Jquery,我在sharepoint网站上有一个表单,希望某些字段是必填字段日期时间选择器等。。 要使一些其他字段可见/隐藏,并在所需字段标题后添加一个红色*,我使用以下示例: //Strategic Priority $('nobr:contains("Strategic Priority")').closest('tr').show(); $('nobr:contains("Strategic Priority")').html("Strategic Priority <span class=\"
//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或name来获取元素并检查它是否为空
提前谢谢
编辑:
我试过了
如果$input[title='Start Date'].val.length<1
{
警报测试;
}
如果字段中有一些数据,它不会给我弹出窗口,但是如果字段为空,它会弹出。
下一步是在字段后面添加一些文本,而不是发出一个提示“必填字段”。如何在我验证的元素之后使用jQuery/JavaScript插入它?类似的东西怎么样
$("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);
}
}
});