In the below code, if I select try1,it should search db and display corresponding value in input box as well as radio button. Here input box value is displayed correctly but radio button is not working. Need help
Script:
$("#test1").change(function()
{
$.post('test_file.php', {test_name: $(this).val() },
function(opt){
$("#name33").val(opt.test_code);
$('#ok').val(opt.test_code2);
}, 'json');
});
Html:
<table><tr>
<td>
<select name="test1" id="test1" >
<option >try1</option>
<option >try2</option>
</select>
</td>
<td>
<input type="text" name="name33" id="name" />
</td>
<td>
<input type="radio" name="ok" id="ok" value="ok"/>ok
<input type="radio" name="ok" id="ok" value="not_ok"/>Not ok
</td>
test_file.php
$db_sql = $db->queryUniqueObject("SELECT * FROM client WHERE test_name='".$_POST['test_name']."'" );
$test_code1=$db_sql->name;
$test_code2=$db_sql->try;
if($db_sql!=NULL)
{
$arr = array ("test_code1"=>"$test_code1","test_code2"=>"$test_code2");
echo json_encode($arr);
}
else
{
$arr1 = array ("no"=>"no");
echo json_encode($arr1);
}
Use
if(opt.test_code2=="ok")
{
$('input:radio[value=ok]').prop("checked",true);
}
else if(opt.test_code2=="not_ok")
{
$('input:radio[value=not_ok]').prop("checked",true);
}
Instead Of
$('#ok').val(opt.test_code2);
Because you set only value using
.val()
function andid
of radio button is not unique so you have to takeradio value
as selector. If you want to checked your radio button according to its value then you have to use.prop()
function.
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.