官术网_书友最值得收藏!

Using JavaScript with custom fields

Just as in the Adding help tips to custom fields recipe, we can also add JavaScript code in the custom field description as long as we wrap the code in the <script> tags.

In this recipe, we will look at another way to remove the None option from select list custom fields.

Getting ready

This recipe uses the jQuery JavaScript library, which is bundled with JIRA. If you are not familiar with jQuery, you can find the documentation at http://jquery.com.

We will also need to use the custom field's ID in our script, so you will need to have that handy. You can find the ID by going to the custom fields page, clicking on the Edit link of the target field, and clicking the number at the end of the URL is the field's ID. For example, the following URL shows a custom field with the ID 10103:

http://jira.localhost.com:8080/secure/admin/EditCustomField!default.jspa?id=10103

How to do it…

Proceed with the following steps to add JavaScript to custom field description:

  1. Log in to JIRA as a JIRA administrator.
  2. Navigate to Administration | Issues | Custom Fields.
  3. Click on the Edit link for the custom field.
  4. Enter the following JavaScript snippets into the Description text box and click on Update. You will need to substitute it in your custom field's ID.
    <script>
    AJS.$('#customfield_10103 option[value="-1"]').remove();
    </script>

    The following screenshot shows that the Team custom field no longer has the None option:

How it works…

In our script, we use jQuery to select the Team custom field based on its element ID and remove the option with value -1 (which is the None option) with the selector #customfield_10103 option[value="-1"].

We use the Atlassian JavaScript (AJS ) namespace (AJS.$), which is the recommended way to use jQuery in JIRA.

主站蜘蛛池模板: 贡嘎县| 宾阳县| 辉南县| 雷山县| 墨脱县| 昭苏县| 黎城县| 农安县| 漳州市| 华安县| 车险| 永寿县| 高青县| 河北省| 文山县| 开江县| 滨州市| 广德县| 黎平县| 文昌市| 繁峙县| 大渡口区| 宝山区| 乐业县| 民勤县| 莫力| 绍兴县| 寿光市| 保康县| 三台县| 揭阳市| 龙里县| 华亭县| 沅陵县| 正蓝旗| 云霄县| 扶余县| 丰镇市| 林口县| 石楼县| 张家川|