如何选择在Ajax回调无线电输入 [英] How to select a radio input on ajax callback
问题描述
我会做一个自动完成表单。其中如地址,电话,传真和部门表格的选项后,用户名被选中下拉(自动提示)列表中自动选择。
I'm going to make a auto complete form. In which the options of the form like address, telephone, fax and department are auto select after a username is selected by a drop down (auto suggest) list.
整个过程就完成了。但只有一件事 - 单选按钮。我没有任何想法如何使它从阿贾克斯价值回归选择。这是我的code。
The whole process is completed. But only one thing - the radio button. I don't have any idea how to make it selected from a value return by ajax. Here is my code.
HTML
<form id="inst_name">
Address : <textarea name="addr" id="addr"></textarea>
Telephone : <input type="text" name="tel" id="tel" />
Department :
<input type="radio" name="dep" id="dep1" value="1" /><label>Dep 1</label>
<input type="radio" name="dep" id="dep2" value="2" /><label>Dep 2</label>
<input type="radio" name="dep" id="dep3" value="3" /><label>Dep 3</label>
</form>
Javascript的
$('#inst_name').blur(function(){
$.ajax({
url: 'inc/autoform.json.php',
dataType:'json',
data: {name:$('#inst_name').val()},
success: function(data){
$('#inst_addr').val(data.addr);
$('#inst_tel').val(data.tel);
$('#hidden_dep').val(data.dep);
}
});
});
例如,如果一个Ajax返回 data.dep =1,在德普1 应选择。
For example, if an ajax returned data.dep="1" so the Dep 1 should be selected.
推荐答案
您可以简单地通过他们循环再检查一个匹配的值。
You can simply loop through them then check the one that matches the value.
$("[name=dep]").each(function(i,v){
var dep2 = $(this).val();
if(data.dep == dep2) $(this).prop("checked",true);
});
这篇关于如何选择在Ajax回调无线电输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!