gpt4 book ai didi

javascript - 如何在单选按钮单击时获取 json 存储的数据

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

我是 javascript 和 php 的初学者。我将单选按钮单击上的 json 数据传递给 js,然后将特定的单选选择传递给我想要获取它并显示在 html 表格上的

$return_arr=array();
$row_array['firstName']= $row['firstName'];
$row_array['lastName']= $row['lastName'];
$row_array['mobiePhoneNumber']= $row['mobiePhoneNumber'];
$row_array['officePhoneNumber']= $row['officePhoneNumber'];

// here push data into json array
array_push($return_arr,$row_array);

$(".radioBtn").click(function(event){
e.preventDefault();
// using data attribute getting data of radio button click (json data)
sendAjaxRequest(($(this).data("jsondata")));
});

function sendAjaxRequest(element,urlToSend)
{
// here element has got whole json data but not able to get one by one data
// I have used JSON.parse(element); but page is not working
var jsondata = element;

alert(jsondata.firstName);

//var jsonDataa = $.parse(jsondata);
alert(jsondata["firstName"]);
}

从 HTML 传递 radio 中选择整个 json 数据

<input type="radio" id="radioBtn" class="radioBtn" name="radioBtn"
data-jsondata=<?php echo json_encode($return_arr)?>>

enter image description here

更新:: 这些代码可以工作,但由于某种原因它不能工作,即。单选按钮位于 foreach 循环内(未从 d 获取完整数据),并且 data-jsondata 数据属性不包含正确的格式,这就是它无法工作的原因,否则它可以工作,谢谢大家!快乐编码

最佳答案

这可能对你有帮助。

// Suppose Your JSON data is {"name":"abcd","age":"22"}  from PHP
<input type="radio" onclick="call(this)" data-jsondata='{"name":"abcd","age":"22"}' /> abc


function call(obj){
var data = obj.getAttribute("data-jsondata");
data = JSON.parse(data);
alert(data.name);
}

Working Example

关于javascript - 如何在单选按钮单击时获取 json 存储的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41760993/

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