简体   繁体   English

根据JSON结果设置选择框的选项

[英]Set option of the Select box according to JSON result

I actually have some questions but I will start with the main one. 我实际上有一些问题,但我将从主要的问题开始。 I want to set the value of Select box on the basis of JSON. 我想基于JSON设置选择框的值。

Here's the HTML in question, 这是有问题的HTML,

<label class="lbl">Office: </label>
    <select tab-index="6" class="required" name="office" class="off">
        <option value="">---------</option>
        <option value="U">London</option>
        <option selected="selected" value="R">Delhi</option>
        <option value="W">Lisbon</option>
    </select>

JSON sends it like this, I can't show the full JSON since it's too big, but I will show a part, Location: "U" . JSON是这样发送的,由于太大,我无法显示完整的JSON,但我将显示一部分, Location: "U"

Here's the JS part: 这是JS部分:

if (data.Office === "R") {
    $('select option[value="Delhi"]').prop('selected', true);
}
if (data.Office === "U") {
    console.log('here');
    $('.off option[value="London"]').attr('selected', 'selected');
}
if (data.Office === "W") {
    $('select option[value="Lisbon"]').prop('selected', true);
}

But it's not working? 但这不起作用吗? Can any one point out why? 谁能指出原因?

Moreover, I have a list of managers say and I am also getting that in JSON. 而且,我有一份经理说的清单,我也用JSON来获取。 So I am doing this, 所以我这样做

for (var i = 0; i < data.Managers.length; i++) {
    find_input = $('input[name="project_manager[]"').length;
    if (find_input != data.Managers.length) {
        $('<input type="text" name="project_manager[]" class="" value="" />').appendTo('#managers');
    }
    console.log(data.Managers[i].Manager);
    $('input[name="project_manager[]"').each(function() {
        $(this).val(data.Managers[i].Manager);
    });
}

No of textboxes depend on the number of managers, but it only sets the value of last item from the array of managers in text boxes that are appended. 文本框的数量取决于管理器的数量,但是它仅在附加的文本框中设置管理器数组中最后一项的值。 Why? 为什么?

Moreover I am not able to set value of textarea in Firefox like this: 而且我不能像这样在Firefox中设置textarea值:

$('textarea#some_id').val(data.Description);

It works in Chrome though. 它可以在Chrome中运行。

First you need to add the character "<" in the beginning of the 3rd option of the select box: 首先,您需要在选择框的第三个选项的开头添加字符“ <”:

<option selected="selected" value="R">Delhi</option>

Now, in the JS code, your problem is that you're using the wrong value. 现在,在JS代码中,您的问题是您使用了错误的值。 Instead of: 代替:

$('select option[value="Lisbon"]').prop('selected', true);

You must use: 您必须使用:

$('select option[value="W"]').prop('selected', true);

I hope it help. 希望对您有所帮助。

I think your selectors should be of the form: 我认为您的选择器应采用以下形式:

$('select option[value="R"]').prop('selected', true);

Note the value is the same as the value in the HTML, not the displayed string (ie 'R' instead of 'Delhi'). 请注意,该值与HTML中的值相同,而不是显示的字符串(即“ R”而不是“ Delhi”)。

Also, you should be using prop() consistently for selected flag, as described here by John Resig. 此外,您应该使用prop()始终如一地为选定的标志,如所描述这里由John Resig的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM