这是我的代码,我想在点击功能中获取元素信息,就像我的注释所说的那样。
for(var i = 0, len = json.length; i < length; i++) {
var curid = json['success'][i]['courier_id'];
var curname = json['success'][i]['courier_name'];
document.getElementById("gsid").value=curname;
var tr="<tr> ";
//here is where i want to use onclick function with curid and curname
var td1="<td>"+"<input type='radio' name='as' value='json['success'][i]['courier_id']' id='curiid' onclick='courierselect(<?php echo $shyam='\'+curname+\''; ?>);'/>"+"</td> ";
var td2="<td id='curnname'>"+json['success'][i]['courier_name']+"</td> ";
var td3="<td>"+json['success'][i]['country_charge']+"</td> ";
var td4="<td>"+json['success'][i]['estimate_delivery_time']+"</td> ";
var td5="<td>"+json['success'][i]['tracking_available']+"</td></tr> ";
$("#results").append(tr+td1+td2+td3+td4+td5);
}
答案 0 :(得分:0)
我发现您的代码存在三个问题
... value='json['success'][i]['courier_id']' ...
你没有用字符串
来完成变量 ... courierselect(<?php echo $shyam='\'+curname+\''; ?>) ...
echo $shyam='\'+curname+\'';
您无法使用JS值设置PHP变量。 Understand why echo
字符串值,因此您必须在curname
附近添加一些引号for(var i = 0, len = json.length; i < length; i++) {
var curid = json['success'][i]['courier_id'];
var curname = json['success'][i]['courier_name'];
document.getElementById("gsid").value = curname;
$("#results").append('<tr> ');
$("#results").append('<td><input type="radio" name="as" value="' + curid + '" id="curiid" onclick="courierselect(\'' + curname + '\');"/></td> ');
$("#results").append('<td id="curnname">' + curname + '</td> ');
$("#results").append('<td>' + json['success'][i]['country_charge'] + '</td> ');
$("#results").append('<td>' + json['success'][i]['estimate_delivery_time'] + '</td> ');
$("#results").append('<td>' + json['success'][i]['tracking_available'] + '</td></tr> ');
}