[英]How can I use javascript to check if a sharepoint list contains a specific word, when he is already have information
on my site I have list with column.在我的网站上,我有列列表。 One like "Status"(Choice column. Drop down menu) and when user change this column on Finished need alert him.一个喜欢“状态”(选择列。下拉菜单),当用户在完成时更改此列时需要提醒他。
HTML code of list and JQuery code (Past on Script Editor on WepPart)列表的 HTML 代码和 JQuery 代码(过去在 WepPart 上的脚本编辑器中)
$(document).ready(function(){ $(".ms-formbody").blur(function() { var Text = $(this).val(); if (Text == "Finished") { alert("Alert"); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <tr> <td width="113" class="ms-formlabel" nowrap="true" valign="top"> <span class="ms-h3 ms-standardheader" id="Status"> <nobr>Status <span title="This is a required field." class="ms-accentText"> *</span> </nobr> </span> </td> <td width="350" class="ms-formbody" valign="top"> <!-- FieldName="Status" FieldInternalName="Status" FieldType="SPFieldChoice" --> <span dir="none"> <select title="Status Required Field" class="ms-RadioText" id="Status_30b2ab26-c1c0-48fd-b190-854b32fe67ca_$DropDownChoice"> <option value="Planned">Planned</option> <option value="Development">Development</option> <option value="Executing">Executing</option> <option selected="selected" value="Finished">Finished</option> <option value="Cancelled">Cancelled</option> </select> <br> </span> <span class="ms-metadata">Choice</span> </td> </tr>
Ok, a few issues here:好的,这里有几个问题:
1) you're selecting completely the wrong element in the jQuery. 1) 您在 jQuery 中选择了完全错误的元素。 Currently it will match a <td
element, but that doesn't have any change or blur events, and it doesn't have a value either.目前它将匹配<td
元素,但它没有任何更改或模糊事件,也没有值。 You need to get hold of the dropdownlist in order to detect when it's changed.您需要掌握下拉列表以检测何时更改。 Given the HTML below, you can use the ms-RadioText
class which the <select
has got.给定下面的 HTML,您可以使用<select
获得的ms-RadioText
类。
2) "blur" is the wrong event to handle - this will only occur if the user moves the focus away from the dropdown after they selected a value. 2)“模糊”是错误的处理事件 - 只有当用户在选择一个值后将焦点从下拉列表中移开时才会发生这种情况。 You can't rely on this actually happening.你不能指望这实际发生。 "change" is the correct event. “变化”是正确的事件。
3) Your exact requirement for what should occur when the select changes is unclear and appears (from the comments) to have changed since the question was posted, so I'm going to give an example where it simply reports the newly selected value in all scenarios. 3)您对选择更改时应该发生什么的确切要求不清楚并且(从评论中)自问题发布以来已经发生变化,所以我将举一个例子,它只是简单地报告新选择的值场景。 If you want to do something more specific than that, you can easily add an if
statement or whatever you need.如果您想做比这更具体的事情,您可以轻松添加if
语句或您需要的任何内容。
Demo:演示:
$(document).ready(function(){ $(".ms-RadioText").change(function() { var Text = $(this).val(); alert(Text); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <tr> <td width="113" class="ms-formlabel" nowrap="true" valign="top"> <span class="ms-h3 ms-standardheader" id="Status"> <nobr>Status <span title="This is a required field." class="ms-accentText"> *</span> </nobr> </span> </td> <td width="350" class="ms-formbody" valign="top"> <!-- FieldName="Status" FieldInternalName="Status" FieldType="SPFieldChoice" --> <span dir="none"> <select title="Status Required Field" class="ms-RadioText" id="Status_30b2ab26-c1c0-48fd-b190-854b32fe67ca_$DropDownChoice"> <option value="Planned">Planned</option> <option value="Development">Development</option> <option value="Executing">Executing</option> <option selected="selected" value="Finished">Finished</option> <option value="Cancelled">Cancelled</option> </select> <br> </span> <span class="ms-metadata">Choice</span> </td> </tr>
Use the code below to achieve it.使用下面的代码来实现它。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select[title='Status Required Field']").change(function(){
if($(this).val()=="Finished"){
alert("Alert");
}
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.