【问题标题】:How to append radio buttons in Javascript?如何在Javascript中附加单选按钮?
【发布时间】:2020-03-16 23:07:56
【问题描述】:

尝试附加单选按钮但显示 [object Object]。我的代码可能有什么问题?

.append('<li>'+JSON.parse(data.responseText).question+' ('+JSON.parse(data.responseText).score+'pts) 
'+$.each(JSON.parse(data.responseText).choices, function(key, value){ {{Form::radio("radio", "value", ["class" => "form-control"])}}' })+' ');

控制台

choices: Array(4)
0: {id: 121, quiz_question_id: 91, choice: "B", created_at: "2019-11-21 
05:35:42", updated_at: "2019-11-21 05:35:42"}
1: {id: 122, quiz_question_id: 91, choice: "G", created_at: "2019-11-21 
05:35:42", updated_at: "2019-11-21 05:35:42"}
2: {id: 123, quiz_question_id: 91, choice: "C", created_at: "2019-11-21 
05:35:42", updated_at: "2019-11-21 05:35:42"}
3: {id: 124, quiz_question_id: 91, choice: "C1", created_at: "2019-11-21 
05:35:42", updated_at: "2019-11-21 05:35:42"}
length: 4

【问题讨论】:

    标签: javascript jquery json laravel laravel-6


    【解决方案1】:

    $.each 不会返回文本。您必须在回调中附加文本。请参考以下示例:

         HTML: 
        <label for="search_field">Address</label>
        <input type='text' class='address_field' />
        <li class="some_class"></li>
    
        Javascript:
    
        $(document).ready(function() {
    
            var arr = [1,2,3];
            $.each(arr, function(key,val) {
                $('.some_class').append("(" + val + " )<li>sample1</li>");
            });
        });
    

    【讨论】:

    • 你能不能做一个console.log(data.responseText);并在这里给我们回复?似乎问题是一个对象而不是一个属性。
    • 我看到您正在迭代选择。这是一个字符串数组吗?
    • choices: Array(4) 0: {id: 121, quiz_question_id: 91,choice: "B", created_at: "2019-11-21 05:35:42", updated_at: "2019 -11-21 05:35:42"} 1: {id: 122, quiz_question_id: 91, 选择: "G", created_at: "2019-11-21 05:35:42", updated_at: "2019-11- 21 05:35:42"} 2: {id: 123, quiz_question_id: 91, 选择: "C", created_at: "2019-11-21 05:35:42", updated_at: "2019-11-21 05: 35:42"} 3: {id: 124, quiz_question_id: 91, 选择: "C1", created_at: "2019-11-21 05:35:42", updated_at: "2019-11-21 05:35:42 "} 长度:4
    • 这就是问题所在。由于选择是一个对象数组,因此您必须在打印值之前进行字符串化。您编写的回调函数(键,值)迭代选择数组。因此,它会一个一个地获取对象,并且必须先对这些对象进行字符串化,然后才能将其放入标记中。
    • @ArunSelin 在我的每个函数中,它都在循环数据。但它显示的是输入 type="radio" 而不是 JSON 中的值
    猜你喜欢
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2020-01-27
    • 2012-11-11
    • 2017-06-13
    • 1970-01-01
    • 2017-10-04
    • 2013-11-25
    相关资源
    最近更新 更多