简体   繁体   English

JSON值未正确附加到HTML

[英]JSON value not getting appended to the Html correctly

I am having a JSON object 我有一个JSON对象

var jsonString =[
    {
        "key": "Monday, June 18, 2012",
        "value": "10 00 AM|_mod,11 00 AM|_mod,12 00 PM|_mod,13 00 PM|_mod"
    },
    {
        "key": "Tuesday, June 19, 2012",
        "value": "13 00 PM|_mod,13 30 PM|_mod,14 00 PM|_mod,14 30 PM|_mod,15 00 PM|_mod"
    }
];

I have two drop-downs I am trying to match the key selected from the first drop-down with the key in the JSON and populate the second drop-down with the appropriate values. 我有两个下拉列表,试图将从第一个下拉列表中选择的键与JSON中的键进行匹配,并用适当的值填充第二个下拉列表。 I want the option tag to look like this 我希望选项标签看起来像这样

<option value="10 00 AM|_mod">10 00 AM</option>

but I am seeing this 但是我看到了

在此处输入图片说明

I think the empty spaces in the 10 00 AM|_mod are causing a problem when the DOM is getting created. 我认为创建DOM时10 00 AM | _mod中的空白会引起问题。

This is my JS code. 这是我的JS代码。

$('#event_date').change(function() {
    var event_date = $("#event_date").val(); // first drop-down val
    var time_type = null;
    var time = null;
    for (var x = 0; x < jsonString.length; x++) {
        if (event_date == jsonString[x].key) {
            var value = jsonString[x].value;
            console.log(value);  // output: 10 00 AM|_mod,11 00 AM|_mod,12 00 PM|_mod,13 00 PM|_mod
            var value_split = value.split(",");
            for (var i = 0; i < value_split.length; i++) {
                console.log(value_split[i]);    // works fine at index 0 I get 10 00 AM|_mod
                time_type = value_split[i].split("|");
                time = time_type[0];
                $('#timeslotIdentifier').append("<option value =" + value_split[i] + ">" + time + "</option>");
            };
        };
    };
});

Is there an encoding problem ? 有编码问题吗? I tried adding breakpoints and it looks fine but when I inspect the element I am seeing this. 我尝试添加断点,看起来不错,但是当我检查元素时,我看到了。

在此处输入图片说明

You're creating an unquoted attribute value. 您正在创建一个未引用的属性值。

You should use jQuery instead: 您应该改用jQuery:

$('<option />').text(time).val(value_split[i]).appendTo('#timeslotIdentifier');
$('#event_date').change(function() {
    var event_date = $("#event_date").val(); // first drop-down val
    var time_type = null;
    var time = null;
    for (var x = 0; x < jsonString.length; x++) {
        if (event_date == jsonString[x].key) {
            var value = jsonString[x].value;
            console.log(value);  // output: 10 00 AM|_mod,11 00 AM|_mod,12 00 PM|_mod,13 00 PM|_mod
            var value_split = value.split(",");
            for (var i = 0; i < value_split.length; i++) {
                console.log(value_split[i]);    // works fine at index 0 I get 10 00 AM|_mod
                time_type = value_split[i].split("|");
                time = time_type[0];
                $('#timeslotIdentifier').append($("<option>").attr({value: value_split[i]}).text(time));
            };
        };
    };
});

jQuery's .val() is definitely better in this case, but in case you don't know where is the error at, it is here: 在这种情况下,jQuery的.val()绝对更好,但是如果您不知道错误在哪里,可以在这里找到:

//Nope. (Missing quotes in HTML)
$('#timeslotIdentifier')
    .append("<option value =" + value_split[i] + ">" + time + "</option>");

//Yep.
$('#timeslotIdentifier')
    .append("<option value =\"" + value_split[i] + "\">" + time + "</option>");

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

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