根据ajax响应检查单选按钮 [英] checked the radio button based on the ajax response
本文介绍了根据ajax响应检查单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个ajax响应,我要根据性别值检查单选按钮
I have this ajax response, what i want is to check the radio button base on the sex value
//fetch_single.php
{
"l_name": "ASDF",
"f_name": "FSDSD",
"m_name": "F",
"sex": "M",
}
这是我的单选按钮,默认情况下选中了男性单选按钮
this is my radio button the male radio button is checked in default
<input class="form-check-input" type="radio" name="sex" id="sex" value="M" checked> Male
<input class="form-check-input ml-1" type="radio" name="sex" id="sex" value="F"> Female
这是我尝试过的
$(document).on('click', '.update', function(){
$('#user_form').parsley().reset();
var user_id = $(this).attr("id");
$.ajax({
url:"../controller/fetch_single.php",
method:"POST",
data:{user_id:user_id},
dataType:"json",
success:function(data)
{
$("input[name^='sex']").find('value="'+data.sex+'"').prop('checked',true);
}
})
});
仍然没有运气,我是初学者,对此表示感谢.
still no luck, im a beginner and im stuck on this thanks.
推荐答案
尝试查找具有匹配值的单选按钮
Try to find the radio button which have the matched value
var data = {
"l_name": "ASDF",
"f_name": "FSDSD",
"m_name": "F",
"sex": "M",
}
$(document).on('click', '.update', function() {
$("input[value='" + data.sex + "']").prop('checked', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="form-check-input" type="radio" name="sex" id="sex" value="M" checked> Male
<input class="form-check-input ml-1" type="radio" name="sex" id="sex" value="F"> Female
<button class="update">Click</button>
注意:这是供实际实现的参考$("input[value='" + data.sex + "']").prop('checked', true);
需要放在success
函数内部
Note: This is for reference for actual implementation $("input[value='" + data.sex + "']").prop('checked', true);
need to be placed inside the success
function
这篇关于根据ajax响应检查单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文