通过JavaScript设置下拉默认设置无效

时间:2018-05-08 13:24:31

标签: javascript jquery freshdesk

我正在尝试将Freshdesk表单上的两个下拉菜单自动设置为特定值。我有一个下拉表示行为,但使用相同的代码不影响第二个下拉列表(值已更改,而不是字面上相同的代码。

工作下拉

<select class="  dropdown_blank" id="helpdesk_ticket_product_id" name="helpdesk_ticket[product_id]"><option value="">...</option>
<option value="36000002397">Stack Overflow Community</option>
<option value="36000001553">Stack Overflow for Teams</option>
<option value="36000002396">Stack Overflow Talent</option></select>
</div> 

document.getElementById('helpdesk_ticket_product_id').value=36000002396;

非工作下拉

<select class=" required nested_field" id="helpdesk_ticket_custom_field_cf_department_856620" name="helpdesk_ticket[custom_field][cf_department_856620]"><option value="">...</option>
<option value="Talent">Talent</option>
<option value="Teams">Teams</option>
<option value="N/A">N/A</option></select><div class="level_2"><label class="required">Category</label><select class=" required nested_field" id="helpdesk_ticket_custom_field_cf_category_856620" name="helpdesk_ticket[custom_field][cf_category_856620]"><option value="">...</option>
</select></div>

document.getElementById('helpdesk_ticket_custom_field_cf_department_856620').value=Talent;

我需要做些额外的事情,因为第二个下拉列表是嵌套的吗?我只想指定第一个下拉列表,但要求用户自己选择第二个嵌套字段。

如果它有帮助,我已经添加了整个脚本部分:

<script type='text/javascript'>

    var getParameterByName = talent

    var talent = ['helpdesk_ticket_custom_field_cf_teams_url_856620']

    if(getParameterByName= 'talent'){
        talent.map(function(field_id){
          jQuery('#'+field_id).parents('.control-group').remove();
        })
     }

     document.getElementById('helpdesk_ticket_product_id').value=36000002396;

     document.getElementById('helpdesk_ticket_custom_field_cf_department_856620').value=Talent;

</script>

document.getElementById上面的函数是一个隐藏某些字段的脚本。

HTML来源:

<div class="page">


    <!-- Search and page links for the page -->


    <!-- Notification Messages -->
     <div class="alert alert-with-close notice" id="noticeajax" style="display:none;"></div> 


    <div class="c-wrapper">     
        <div class="new_ticket_page">
        <section class="content main rounded-6 fc-new-ticket">
    <h3 class="heading">Submit a ticket</h3>
    <form accept-charset="UTF-8" action="/support/tickets" class="form-portal ticket-form" enctype="multipart/form-data" id="new_helpdesk_ticket" method="post" rel="validate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="3xOZ+O420PsIskGZYjEEpcyo8pJDgvtEaAVR5YCqsF0=" /></div>


      <div class="control-group " >
         <label class=" required control-label requester-label " for="helpdesk_ticket_email">Requester</label>
            <div class="controls   ">
              <div class="row-fluid">
    <input class="span12 email required" data-check-email-path="/support/tickets/check_email" data-remote-triggers="focusout" data-remote="true" id="helpdesk_ticket_email" name="helpdesk_ticket[email]" placeholder="Email" size="30" type="email" value="" />

    <div id="name_field" class="default_name_field hide">
        <input class="text span12 name_field" placeholder="Your Name" 
            name="helpdesk_ticket[name]" type="text" disabled="disabled" />
    </div>

</div>


            </div> 
      </div>
      <div class="control-group " >
         <label class=" required control-label subject-label " for="helpdesk_ticket_subject">Subject</label>
            <div class="controls   ">
              <input class=" required text span12" id="helpdesk_ticket_subject" name="helpdesk_ticket[subject]" size="30" type="text" />
            </div> 
      </div>
      <div class="control-group " >
         <label class="  control-label product-label " for="helpdesk_ticket_product">Product</label>
            <div class="controls   ">
              <select class="  dropdown_blank" id="helpdesk_ticket_product_id" name="helpdesk_ticket[product_id]"><option value="">...</option>
<option value="36000002397">Stack Overflow Community</option>
<option value="36000001553">Stack Overflow for Teams</option>
<option value="36000002396">Stack Overflow Talent</option></select>
            </div> 
      </div>
      <div class="control-group " >
         <label class=" required control-label cf_department_856620-label " for="helpdesk_ticket_cf_department_856620">Product Categories</label>
            <div class="controls nested_field  ">
              <select class=" required nested_field" id="helpdesk_ticket_custom_field_cf_department_856620" name="helpdesk_ticket[custom_field][cf_department_856620]"><option value="">...</option>
<option value="Talent">Talent</option>
<option value="Teams">Teams</option>
<option value="N/A">N/A</option></select><div class="level_2"><label class="required">Category</label><select class=" required nested_field" id="helpdesk_ticket_custom_field_cf_category_856620" name="helpdesk_ticket[custom_field][cf_category_856620]"><option value="">...</option>
</select></div><script type="text/javascript">
//<![CDATA[
jQuery('#helpdesk_ticket_custom_field_cf_department_856620').nested_select_tag({"data_tree":[["Talent","Talent",[["API &amp; Integrations","API &amp; Integrations",[]],["Banner Ads","Banner Ads",[]],["Candidate Search","Candidate Search",[]],["Company Page Ads","Company Page Ads",[]],["Company Pages","Company Pages",[]],["Control Panel","Control Panel",[]],["Dashboard","Dashboard",[]],["Job Listing","Job Listing",[]],["N/A","N/A",[]],["Recommended Candidates","Recommended Candidates",[]],["User &amp; Team Accounts","User &amp; Team Accounts",[]]]],["Teams","Teams",[["Administration","Administration",[]],["Billing","Billing",[]],["Technical","Technical",[]],["Usage","Usage",[]]]],["N/A","N/A",[["N/A","N/A",[]]]]],"initValues":null,"disable_children":false,"include_blank":"...","selected":null,"pl_value_id":null,"subcategory_id":"helpdesk_ticket_custom_field_cf_category_856620"});
//]]>
</script>
            </div> 
      </div>
      <div class="control-group " >
         <label class=" required control-label cf_customer_account_856620-label " for="helpdesk_ticket_cf_customer_account_856620">Control Panel, Salesforce, or Talent link.</label>
            <div class="controls   ">
              <input class=" required text span12" id="helpdesk_ticket_custom_field_cf_customer_account_856620" name="helpdesk_ticket[custom_field][cf_customer_account_856620]" size="30" type="text" />
            </div> 
      </div>
      <div class="control-group " >
         <label class=" required control-label cf_teams_url_856620-label " for="helpdesk_ticket_cf_teams_url_856620">Teams URL</label>
            <div class="controls   ">
              <input class=" required text span12" id="helpdesk_ticket_custom_field_cf_teams_url_856620" name="helpdesk_ticket[custom_field][cf_teams_url_856620]" size="30" type="text" />
            </div> 
      </div>
      <div class="control-group " >
         <label class=" required control-label description-label " for="helpdesk_ticket_description">Description</label>
            <div class="controls   ">
               <textarea class=" required_redactor html_paragraph span12" cols="40" id="helpdesk_ticket_ticket_body_attributes_description_html" name="helpdesk_ticket[ticket_body_attributes][description_html]" rows="6">
</textarea>  <div class="single_file">
<div class="attach-wrapper row-fluid new-attach" id="attachment-type" data-multifile-enable="true" data-location="portal">
  <div class="attachments-list-wrap">
    <div class="attachments-list-form" id="ticket-container">

      <div class="attachment_contents">
        <div id="ticket-attachments" class="attachments-wrap shared_attachment_list">



        </div>
      </div>
    </div>

1 个答案:

答案 0 :(得分:0)

由于您的价值(&#34; Talent&#34;)是一个字符串,您必须使用

document.getElementById('helpdesk_ticket_custom_field_cf_department_856620').value='Talent';

另一种方法:

document.getElementById('helpdesk_ticket_custom_field_cf_department_856620').querySelector('option[value="Talent"]').selected = true;

修改

您的代码中还有一些问题。它应该是:

<script type='text/javascript'>

    var getParameterByName = 'talent';

    var talent = ['helpdesk_ticket_custom_field_cf_teams_url_856620'];

    if(getParameterByName === 'talent'){
        talent.map(function(field_id){
          jQuery('#'+field_id).parents('.control-group').remove();
        });
     }

     document.getElementById('helpdesk_ticket_product_id').value='36000002396';

     document.getElementById('helpdesk_ticket_custom_field_cf_department_856620').value='Talent';

</script>

在第3行中,您错过了(单个)引号。

在第7行中,正确的运算符是&#34; ==&#34;或&#34; ===&#34; (见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

在第15行中你也错过了单引号(实际上它们也应该在第13行中使用,因为 value s总是字符串。)