I need get value for radio button from sql server using ajax ,php and represent value in form contain radio here code ajax
function getname()
{
var id=$("#idEmploye").val(); // get the id from textbox
$.ajax({
type:"post",
dataType:"json",
data:"id="+id,
url:"page.php", // url of php page where you are writing the query
success:function(json)
{
$("#gender").val(json.gender).prop('checked',true)
}
});
}
code html here
<label >Gender</label>
<input name="gender" id="gender" type="radio" value="Male" >Male</input>
<input name="gender" id="gender" type="radio" value="Female">Female</input>
currently can show value in textbox not in radio button but I need show value in radio button any help
Very Thanks
Try this,
$("input:radio[name='gender'][value='"+ json.gender +"']").attr('checked', true);
or
$("input:radio[name='gender'][value='"+ json.gender +"']").prop('checked', true);
Then each input
elements of id
should be unique. Please remove the repetitive id
. Use name
property behalf of id
Basically two html elements should not have same id. So change your HTML like
<label >Gender</label>
<input name="gender" id="Male" type="radio" value="Male" >Male</input>
<input name="gender" id="Female" type="radio" value="Female">Female</input>
The you can set the value with js
var gender = element = document.getElementById(json.gender);
gender.prop('checked');
If you are using an older jquery version (< 1.6)
gender.attr('checked');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.