Assumptions:
The value to be set is passed in the query string url. Example:
<SiteURL>/sites/newform.aspx?ID=3&RollNo=10
The list has a drop down field say “ID”.
The list has a single line text field say “Roll No”.
Steps:
- Open the newform.aspx page of a list.
- Edit the page.
- Add a new script editor webpart.
- Add the below code to the script editor webpart.
- <script src="/sites/test/SiteAssets/jquery-1.10.2.js"></script>
- < script src="/sites/test/SiteAssets/sputility.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function()
- {
- var ID = GetUrlKeyValue('ID');
- var rollNo = GetUrlKeyValue('RollNo');
- $("Select[Title='ID']").val(ID);
- $("Select[Title='ID']").prop("disabled", "disabled");
-
- $("input[Title='Roll No']").val(rollNo);
- $("input[Title='Roll No']").prop("disabled", "disabled");
-
- });
- </script>
Explanation:
- Reference needs to be made to jquery and SPUtility.js files.
- GetURLKeyValue(‘<parameter>’) – gets the value from the query string.
- $("Select[Title='ID']").val(ID); à "select" is used for drop down fields, its setting the value as per the query string value.
- $("input[Title='Roll No']").val(rollNo); à”input” is used for text fields and is setting the value as per the query string parameter.
- $("Select[Title='ID']").prop("disabled", "disabled"); à After setting the value this line, is setting the fields as disabled.