如何动态地使用jQuery在select选项中设置值?

时间:2016-05-26 12:58:14

标签: jquery html-select

下面我给出了代码的快照。在这里,我遇到了问题,因为在某些下拉值中使用下面的代码设置但是某些下拉列表没有设置。请告诉我哪里出错了。

function templateLoading(value){

    alert(value);

    $.ajax({
        type : "GET",
        contentType : "application/json",
        url : "templateload.ank?name="+value,
        dataType : 'json',
        success : function(data) {

            alert(data);
            $.each(data,function(key, val) {
                //alert("fc"+"'"+key+"'");

                if(key=="fileHeaderAvailbale"){
                    alert(key +"fileHeaderAvailbale");



                }

//在密钥中 - >动态组件ID即将到来"值"我之前设定的价值即将到来。我想通过使用通过密钥传入的id将所有这些先前的值设置为相关组件。

                    $("#"+key).val(val);


                //document.getElementById(key).value=val;

            });
            console.log("SUCCESS: ", data);
        },
        error : function(e) {
            //document.getElementById("FooterConstant").value= "rama";  
            console.log("ERROR: ", e);

        },
    })
}

我的HTML代码如下。此HTML代码属于问题页面,当您单击以下链接时,该问题页面将显示在图像中。

Click here to see Screen shot of the problem.

<fieldset>
            <legend>Header Details tab Configuration</legend>

            <table style="width: 100%">
                <tr>
                    <td><label for="fheader">File Header Available: </label></td>
                    <td><select id="fileHeaderAvailbale" onchange="fileHeaderAvail(value);" name="fheader">
                             <option value="default">Select</option> 
                            <option value="YES">YES</option>
                            <option value="NO">NO</option>
                    </select></td>

                    <td><label for="dupHeader">Duplicate Check Header:</label></td>
                    <td><select id="duplicateCheckHeader" disabled="disabled" name="dupHeader">
                            <option value="default">Select</option>
                            <option value="YES">YES</option>
                            <option value="NO">NO</option>
                    </select></td>

                    <td><label for="mlHeader">MultiLine Header: </label></td>
                    <td><select id="multilineHeader"
                        onchange="multiLineHeaderAvail(value);" disabled="disabled" name="mlHeader">
                            <option value="default">Select</option>
                            <option value="YES">YES</option>
                            <option value="NO">NO</option>
                    </select></td>


                </tr>

                <tr>
                    <td><label for="headerType">Header Key Type:</label></td>
                    <td><select id="headerKeyType"
                        onchange="multipleHeaderDataCountSet(value);" disabled="disabled" name="headerType">
                            <option value="default">Select</option>
                            <option value="SingleHeaderKey">SingleHeaderKey</option>
                            <option value="MultipleHeaderKey">MultipleHeaderKey</option>
                    </select></td>

                    <td><label for="mheaderDataCount">Multiple Header
                            Data Count: </label></td>
                    <td><input id="multipleHeaderDataCount" name="mheaderDataCount"
                        type="text" class="required" disabled="disabled"></td>


                    <td><label for="lnkItem">Link Item changes to link</label></td>

                    <td><a href="#" id="lnkItem">Header Details</a></td>

                </tr>


                <tr>
                    <td><label for="lineHeaderRecord">Line Between Header
                            and Data Record: </label></td>
                    <td><input id="lineBetweenHeaderData" name="lineHeaderRecord"
                        type="text" class="required"></td>

                    <td><label for="headerSequenceCheck">Header Sequence
                            Check Applicable: </label></td>
                    <td><select id="headerSequenceCheck" disabled="disabled"
                        onchange="hdrSeqChkApplicable(value);" name="headerSequenceCheck">
                            <option value="default">Select</option>
                            <option value="YES">YES</option>
                            <option value="NO">NO</option>
                    </select></td>

                    <td><label for=headerSequenceFormat> Header Sequence
                            Format: </label></td>
                    <td><input id="headerSequenceFormat"
                        name="headerSequenceFormat" type="text" class="required"
                        disabled="disabled"></td>

                </tr>

                <tr>
                    <td><label for="dataRecord">Data Record Between
                            Header:</label></td>
                    <td><input id="datarecordBetweenHeader" name="dataRecord" type="text"
                        disabled="disabled" class="required"></td>

                    <td><label for="headerAvail">Header Tag Available :</label></td>
                    <td><select id="headerTagAvailbale"
                        onchange="headerTagAvail(value);" disabled="disabled" name="headerAvail">
                            <option value="default">Select</option>
                            <option value="YES">YES</option>
                            <option value="NO">NO</option>
                    </select></td>

                    <td><label for=headerTagName> Header Tag Name: </label></td>
                    <td><input id="headertagName" name="headerTagName"
                        disabled="disabled" type="text" class="required"></td>

                </tr>

                <tr>
                    <td><label for=childTagCount> Header Child Tag Count:
                    </label></td>
                    <td><input id="headerChildTagCount" name="childTagCount"
                        disabled="disabled" type="text" class="required"></td>



                    <td><label for=dataElement>Header Data Element: </label></td>
                    <td><input id="headerdataElement" name="dataElement" type="text"
                        disabled="disabled" class="required"></td>

                </tr>

            </table>
        </fieldset>


数据采用JSON格式,如下所示。

    {
    "fileName": "IRIS",
    "sequenceNumber": 0,
    "constvalue": "12",
    "databeginCheck": "Yes",
    "databeginConst": "NO",
    "datarecordBetweenHeader": "13",
    "datarecordConst": "Yes",
    "datawithMulticonst": "50",
    "defineConst": "asd",
    "delimeter": ",",
    "duplicateCheck": "YES",
    "duplicateCheckHeader": "",
    "fileBeginWith": "IRIS2015",
    "fileBeginWithCheck": "YES",
    "fileHeaderAvailbale": "Yes",
    "fileId": 0,
    "fileLocation": "Local",
    "fileNameConvention": "DD:MM:yyyy",
    "fileNameConventionCheckApplicable": "NO",
    "fileNameLength": "15",
    "filePath": "D://IRIS/2015/ABC",
    "fileShortName": "IRIS125",
    "fileType": "Delimited",
    "filefooterAvailable": "YES",
    "filefooterWithConst": "YES",
    "footerConst": "asdfg",
    "footerFormat": "YYMMDDHHMI",
    "footerFormateCheck": "YYMMDDHHMI",
    "footerLength": "16",
    "footerType": "FixedLength",
    "headerChildTagCount": "No",
    "headerKeyType": "asce",
    "headerSequenceCheck": "No",
    "headerSequenceFormat": "3",
    "headerTagAvailbale": "12",
    "headerdataElement": "uihk",
    "headertagName": "Yes",
    "lineBetweenFooterData": "15",
    "lineBetweenHeaderData": "MultipleHeaderKey",
    "linebetweenDataRecord": "14",
    "multilineHeader": "No",
    "multilineRecordCheck": "NO",
    "multipleHeaderDataCount": "15",
    "noTagcontrol": "15",
    "numberConstant": "01",
    "numberLine": "41",
    "numberOfColumns": "10",
    "recordcountLine": "Yes",
    "selectResources": "null"
}


我的屏幕在下面,我评论了价值没有设定的地方。下面我提供我的图像链接,请点击此屏幕截图,告诉我哪里出错了。

Click here to see Screen shot of the problem.

谢谢&amp;问候,
Shivam

2 个答案:

答案 0 :(得分:0)

这是代码:

下拉列表中的值是大写的,这就是为什么不选择它。

$.each(data,function(key, val) {     
   var $dd = $("#"+key);
   if($dd && $dd.length > 0){
      console.log(key, val);
      $dd.val(val.toUpperCase());
   }
});

小提琴: http://jsfiddle.net/yNw3C/13749/

答案 1 :(得分:0)

您可以尝试这样的事情

$('#headerSequenceCheck option:selected').val('No');