在文档加载时使用JQuery在下拉字段中设置默认值

时间:2014-12-23 12:48:23

标签: javascript jquery html

我正在尝试在加载页面时在下拉字段中设置默认值。但是我无法设置该值。我已尝试相同的另一个字段,它工作正常。下面是它可以正常工作的字段的HTML代码和JQuery代码。

HTML代码:

<div class="form-field select required request_type">
<label for="request_type">Type</label>
<a class="nesty-input" tabindex="0">Problem</a>
<select id="request_type" name="request[type]" style="display: none;">
<option value="-">-</option>
<option value="question">Question</option>
<option value="problem">Problem</option>
</select>
<p>Request type</p>
</div> 
<div class="form-field string required request_custom_fields_21062749">
<label for="request_custom_fields_21062749">Severity</label>
<input id="request_custom_fields_21062749" type="hidden" name="request[custom_fields][21062749]" data-tagger="[{"label":"-","value":""},{"label":"Low","value":"lowsev"},{"label":"Medium","value":"mediumsev"},{"label":"High","value":"highsev"},{"label":"Critical","value":"criticalsev"}]" autocomplete="off" value="mediumsev">

Jquery的:

var severityId =21062749;
$(document).ready(function() {
    var ct_tags = HelpCenter.user.tags; 
    if(location.pathname == "/hc/en-us/requests/new" || location.pathname == "/hc/en-us/requests") { 
        if(ct_tags == "product"){
            $('select#request_type').val("problem");// This value is displayed in the form
            $('select#request_custom_fields_'+severityId).val("mediumsev");//This value is not displayed.
        }
    }
});

此致 Shaik。

1 个答案:

答案 0 :(得分:0)

以这种方式设置:

$(document).ready(function() { $('#request_custom_fields_21062749').val("meddfiudfgdjlfgmsev"); });

所以我想你的if语句是个错误!