gpt4 book ai didi

javascript - 使用来自 JSON 的数据检查单选按钮

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

我有以下单选按钮:

<td valign="top">
<label>Have you ever been tested?</label>

<input required id="Test" name="Test" type="radio" class="wellness_center radiostyle" value="1">
<div class="radiolabel">Yes</div>
<input required id="Test" name="Test" type="radio" class="wellness_center radiostyle" value="0">
<div class="radiolabel">No</div>
</td>

以0和1的形式向数据库提交数据(1=是,0=否)。我想从数据库中提取此信息,并根据保存在数据库中的值重新映射/突出显示单选按钮。我尝试了以下方法:

$.ajax({
type: "GET",
url: "database/Emarps/websrvc/websrvc.php?task=getData&UUID=" + currentuuid + "&DataGt=hk",
dataType: "JSON",
success: function (response) {
$.each(response, function (i, resp) {

if (resp.tested === $("input[name='Test'][value='1']").prop('checked', true)) {

} else if (resp.tested === $("input[name='Test'][value='0']").prop('checked', true)) {

}

});
}
});

它可以很好地从数据库中检索数据,但无法将其映射到相应的单选按钮。请协助,我如何根据通过 JSON 传递的数据库中的值将其映射回正确的 radio ?

最佳答案

为了比较,您可以简单地使用文字:

if (resp.tested === "1") {
$("input[name='Test'][value='1']").prop('checked', true);
}

你可以试试这个:

$("input[name=Test]").prop('checked', false); //uncheck all
$.each(response, function (i, resp) {
$("input[name=Test][value=" + resp.tested + "]").prop('checked', true);
}

关于javascript - 使用来自 JSON 的数据检查单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32963215/

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