Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置下拉列表的选定值_Javascript_Jquery - Fatal编程技术网

Javascript 设置下拉列表的选定值

Javascript 设置下拉列表的选定值,javascript,jquery,Javascript,Jquery,我有一个带有值的下拉列表 function getTests() { var html = ''; $.get('/getTests', function (tests) { selTest = tests; html +=' <option value="">SELECT</option>'; tests.forEach(function (test) { html += '<option va

我有一个带有值的下拉列表

function getTests() {
  var html = '';
  $.get('/getTests', function (tests) {
       selTest = tests;
       html +=' <option value="">SELECT</option>';
       tests.forEach(function (test) {
          html += '<option value="' + test.id + '">' + test.names + '</option>'; 
       });
       $('#testnames').html(html);
  });
}
根据testid=25选择作为下拉值“电子邮件确认”

为此,我正在做以下工作:-

$(document).ready(function () {
    if (window.location.href.indexOf('testid') > -1){

       var testid = window.location.href;
       testid =testid.split('=');
       alert($("#testnames").val(testid[1]))
    }
});

但是,如果不获取所选的值,则此处不需要警报,只需使用:

$("#testnames").val(testid[1]);

由于您是使用ajax请求加载内容的,因此需要在ajax请求完成并创建选项后设置值

所以解决方案模板可能看起来像

function getTests(testId) {
    var html = '';
    $.get('/getTests', function (tests) {
        selTest = tests;
        html += ' <option value="">SELECT</option>';
        tests.forEach(function (test) {
            html += '<option value="' + test.id + '">' + test.names + '</option>';

        });
        $('#testnames').html(html);
        if(testId){
            $("#testnames").val(testId)
        }
    });
}

$(document).ready(function () {
    if (window.location.href.indexOf('testid') > -1) {
        var testid = window.location.href;
        testid = testid.split('=');
        getTests(testid[1])
    }else{
        getTests()
    }
});
试试这个脚本

JS
您需要在ajax调用完成后调用此函数,这里的情况不同,js函数首先调用,html随后呈现。因此,我们应该首先呈现html,然后调用js函数,这一点很重要。这对你有用

alert($("#testnames").val(testid[1]).val());
使用此代码 我已经测试过了


var val=$.lngcombo.val;alertval

检查我的小提琴,如果没有,则说明您的window.location.href值或此处生成的HTML可能有问题。我尝试使用您的代码。同时从另一个具有值的页面重定向http://localhost:9080/history.html?testid=25 价值没有得到应有的重视slected@Sush能否在iftestId之前添加警报{并查看testid的值是什么?在我发出警报之前,testid警报testid值25或72或44。如果我发出警报,它会先发出警报testid值,如25,然后发出警报为未定义。当第一次发出警报时,它的值似乎是未定义的。然后发出警报未定义和选定的值不会显示为什么值会在第二次发出警报未定义
function getTests(testId) {
    var html = '';
    $.get('/getTests', function (tests) {
        selTest = tests;
        html += ' <option value="">SELECT</option>';
        tests.forEach(function (test) {
            html += '<option value="' + test.id + '">' + test.names + '</option>';

        });
        $('#testnames').html(html);
        if(testId){
            $("#testnames").val(testId)
        }
    });
}

$(document).ready(function () {
    if (window.location.href.indexOf('testid') > -1) {
        var testid = window.location.href;
        testid = testid.split('=');
        getTests(testid[1])
    }else{
        getTests()
    }
});
$(document).ready(function () {
    if (window.location.href.indexOf('testid') > -1){
        var testid = getParameterByName('testid');
        $('#testnames').val(testid);
    }
});

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
alert($("#testnames").val(testid[1]).val());