编辑:
包含名为services.json的Json文件:
{
"0": "Dog",
"1": "Cat",
"2": "Pony"
}
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"> </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"> </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我正在尝试:
<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>
我尝试了下面的代码但由于某种原因它不会起作用。这似乎削减了很多不相关的剧本,但即使这样也无法奏效。我当时打算使用这种方法为每个按钮创建一个脚本。 我想要的是,一旦选择了狗单选按钮,当猫,1和小马2时,0将显示在跨度中。
我做错了什么?
由于
答案 0 :(得分:2)
我知道这个话题可能已经死了,但我认为你的问题已经存在
这一行var json = $.parseJSON(data);
$('#displayresults').html(json.dog);
您的对象没有属性“dog”,因此您必须将其称为json[1]
或json['1']
。
答案 1 :(得分:0)
您可以使用jQuery绑定到无线电点击事件,然后匹配标签中的值(或将无线电value
更改为返回值 - 即'Dog','Fish','Rabbit')和然后用它来得到你的“结果”。
示例:
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"> </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"> </label>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios3" value="Rabit">
<label>Rabbit</label>
</div>
</div>
<div id="displaydata"></div>
[编辑 - 有关JQuery的信息.ajax()
]
在您的评论中,您使用jQuery的.getJSON()
函数。 Per the documentation这只是简写等同于:
// example:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
......其中:
发送到服务器的数据[
data
]作为查询附加到URL 串。如果data参数的值是普通对象,则为 转换为字符串并进行url编码后再将其附加到 URL。
请查看.getJSON()
文档[上面链接]和.ajax()
documentation。
下面是一些伪代码,它们有效地描述了如何处理获取ajax结果并使用jquery显示它:
[注意 - 如果您还有针对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"> </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"> </label>
<div class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios3" value="Rabbit">
<label>Rabbit</label>
</div>
</div>
<div id="displaydata"></div>