gpt4 book ai didi

javascript - 使用 jquery 根据下拉选择选择单选按钮

转载 作者:行者123 更新时间:2023-11-29 00:03:52 25 4
gpt4 key购买 nike

在下面的代码中,如果我选择try1,它应该搜索数据库并在输入框和单选按钮中显示相应的值。此处输入框值显示正确,但单选按钮不起作用。需要帮助

脚本:

$("#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);

}

最佳答案

使用

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);
}

代替

$('#ok').val(opt.test_code2);

Because you set only value using .val() function and id of radio button is not unique so you have to take radio value as selector. If you want to checked your radio button according to its value then you have to use .prop() function.

Demo

关于javascript - 使用 jquery 根据下拉选择选择单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28427151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com