简体   繁体   English

选择单选按钮后调用JSON

[英]Calling JSON once radio button selected

Edit: 编辑:

Json file containing called services.json: 包含名为services.json的Json文件:

{
"0": "Dog",
"1": "Cat",
"2": "Pony"
}

Html: HTML:

   <form class="form-horizontal">
                    <div class="form-group" id="radiobuttongroup">
                        <label for="inputservice" class="col-sm-2 control-label">Service</label>
                            <div class="radio">
                                <label>
                                <input type="radio" class="buttondog" name="optionsRadios" id="optionsRadios1" value="Dog">
                                Dog</label>
                            </div>
                        <label for="inputservice" class="col-sm-2 control-label">&nbsp</label>
                            <div class="radio">
                                <label>
                                <input type="radio" class="buttoncat" name="optionsRadios" id="optionsRadios2" value="Cat">
                                Cat</label>
                            </div>
                        <label for="inputservice" class="col-sm-2 control-label">&nbsp</label>
                            <div class="radio">
                                <label>
                                <input type="radio" class="buttonpony" name="optionsRadios" id="optionsRadios3" value="Pony">
                                Pony</label>
                            </div>
                        </div>
                    <span id="displayresults"></span>

Jquery im trying: jQuery的即时通讯尝试:

    <script type="text/javascript">
$(document).ready(function() {
     $('.buttondog').click(function(){
        $.ajax({
            url: "services.json",
            dataType: "text",
            success: function(data){
                var json = $.parseJSON(data);
                     $('#displayresults').html(json.dog);
                }
            });
        });
    });
</script>

I tried the code below but for some reason it wouldn't work. 我尝试了下面的代码,但由于某种原因它无法正常工作。 This seemed to cut a lot of the irrelevant script, but even this isnt working. 这似乎削减了许多不相关的脚本,但即使这样也不起作用。 I was then intending to make a script for each button using this method. 然后,我打算使用此方法为每个按钮创建一个脚本。 What i wanted was that once the dog radio button was selected, 0 would display in the span, when cat, 1 and when pony 2. 我想要的是,一旦选择了“狗”单选按钮,则在跨度中将显示0,而在猫,1和小马2的时候。

Am i doing something completely wrong? 我做错什么了吗?

Thanks 谢谢

I know that the topic is probably dead, but I think that your problem is in this line var json = $.parseJSON(data); $('#displayresults').html(json.dog); 我知道这个话题可能已经死了,但是我认为您的问题是在这行中var json = $.parseJSON(data); $('#displayresults').html(json.dog); var json = $.parseJSON(data); $('#displayresults').html(json.dog); Your object doesn't have property 'dog', so you have to call it like json[1] or json['1'] . 您的对象没有属性'dog',因此您必须像json[1]json['1']一样调用它。

You can bind to the radio click event using jQuery, then match the value in the label (or change the radio value to the return value - ie 'Dog', 'Fish', 'Rabbit') and then use that to get your "results". 您可以使用jQuery绑定到单选点击事件,然后匹配标签中的值(或将单选value更改为返回值,即“狗”,“鱼”,“兔子”),然后使用它们来获取“结果”。

Example: 例:

 var data = { "result1": "Dog", "result2": "Fish", "result3": "Rabbit" }; $('input[name="optionsRadios"]').click(function(){ var radioVal = $(this).val(); // <-- if you change input val var labelVal = $(this).next('label').html(); //<-- get the label HTML // get the correct item from the data object $.each(data, function(k,v){ if(v === labelVal){ // added to results div $('#displaydata').html(k + " " + v); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group" id="radiobuttongroup"> <label for="inputservice" class="col-sm-2 control-label">Service</label> <div class="radio"> <label> <input type="radio" name="optionsRadios" id="optionsRadios1" value="Dog"> <!-- <-- Changed value --> <label>Dog</label> </div> <label for="inputservice" class="col-sm-2 control-label">&nbsp</label> <div class="radio"> <label> <input type="radio" name="optionsRadios" id="optionsRadios2" value="Fish"> <label>Fish</label> </div> <label for="inputservice" class="col-sm-2 control-label">&nbsp</label> <div class="radio"> <label> <input type="radio" name="optionsRadios" id="optionsRadios3" value="Rabit"> <label>Rabbit</label> </div> </div> <div id="displaydata"></div> 

[Edit - Info on JQuery .ajax() ] [关于JQuery .ajax()编辑信息]

In your comment, your using jQuery's .getJSON() function. 在您的评论中,您使用的是jQuery的.getJSON()函数。 Per the documentation this is just the shorthand equivalent to: 根据文档,这只是等效的简写:

// example:
$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

... where: ...其中:

Data [ data ] that is sent to the server is appended to the URL as a query string. 发送到服务器的数据[ data ]作为查询字符串附加到URL。 If the value of the data parameter is a plain object, it is converted to a string and url-encoded before it is appended to the URL. 如果data参数的值是纯对象,则将其转换为字符串并经过url编码,然后再附加到URL。

Please take a look at both the .getJSON() documentation [linked above] and .ajax() documentation . 请同时看一下[ .getJSON()文档[链接到上方]和.ajax() 文档

Below is some pseudo-code that effectively describes how you could handle getting an ajax result and displaying it using jquery: 下面是一些伪代码,有效地描述了如何处理获取ajax结果并使用jquery显示它的方法:

[ Note - if you have further questions specific to ajax or need help troubleshooting your request, please open that topic as a new question and provide an example of what you've tried ] [ 注意-如果您对ajax有其他疑问,或者需要帮助来解决您的请求,请将该主题作为一个新问题打开,并提供您尝试过的示例 ]

 // psuedo code for ajax request // setup: // global variable for results object so it can be used by other functions // inentionally left as 'undefined' for debugging: var results; // radio click event: $('input[name="optionsRadios"]').click(function() { var radioVal = $(this).val(); //see helper function below // adds response objec to 'results` variable getObject(radioVal); $('#displaydata').html(results); }); // seperate function for ajax call function getObject(id) { var reqData = { "id" : id }; // fake ajax: $.ajax({ dataType: "json", url: "/objContoller/", data: reqData, success: function(data){ // here's where you handle success! // data can be accessed exactly how it was in the previous example // you can parse it and... // global variable assignment: results = JSON.parse(data); // or just return the json // results = data; }, error: function(response){ // always handle the error.. // how you deal with this is based on server side code alert("this is just an example, so it will always throw this error!!! ID: " + id + " || reqData: " + JSON.stringify(reqData)); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group" id="radiobuttongroup"> <label for="inputservice" class="col-sm-2 control-label">Service</label> <div class="radio"> <input type="radio" name="optionsRadios" id="optionsRadios1" value="Dog"> <label>Dog</label> </div> <label for="inputservice" class="col-sm-2 control-label">&nbsp</label> <div class="radio"> <input type="radio" name="optionsRadios" id="optionsRadios2" value="Fish"> <label>Fish</label> </div> <label for="inputservice" class="col-sm-2 control-label">&nbsp</label> <div class="radio"> <input type="radio" name="optionsRadios" id="optionsRadios3" value="Rabbit"> <label>Rabbit</label> </div> </div> <div id="displaydata"></div> 

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

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