我无法将AJAX加载的表单中的无线电值转换为div。 这是我正在使用的JavaScript代码: (收音机名称是get函数中的'categorie_add'):
function getCheckedValue(radioObj) {
if(!radioObj)
return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
if(radioObj.checked)
return radioObj.value;
else
return "";
for(var i = 0; i < radioLength; i++) {
if(radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
}
function get(obj) {
var poststr ="cat_title=" + escape(encodeURI(document.getElementById("cat_title").value )) +
"&cat_description=" + escape(encodeURI( document.getElementById("cat_description").value ))+
"&cat_id=" + escape(encodeURI( getCheckedValue(document.categorie_add.cat_id) ));
makePOSTRequest('categorie.php', poststr);
}
我在PHP文件中有这个:
echo '<li><input type="radio" name="cat_id" id="cat_id" value="' . $value['cat_id'] . '" /> ' . htmlentities($value['cat_title']) . '<br />';
答案 0 :(得分:1)
我认为问题在于你在DOM中捕捉无线电对象的方式....
请写下代码如何调用函数getCheckedValue
。
$('#cat_id:checked').val();
Jquery是Rock。
function getCheckedValue(radioObj) {
for ( var i in radioObj )if (radioObj[i].checked) return radioObj[i].value;
return false;
}
答案 1 :(得分:1)
您可能需要阅读本教程:
http://www.w3schools.com/jsref/prop_radio_checked.asp
http://www.w3schools.com/html/html_forms.asp
http://www.w3schools.com/htmldom/dom_methods.asp
答案 2 :(得分:0)
我想问题就是你生成单选按钮的方式:
echo '<li><input type="radio" name="cat_id_' . $value['cat_id']
. '" id="cat_id_' . $value['cat_id']
. '" value="' . $value['cat_id'] . '" /> '
. htmlentities($value['cat_title']) . '<br />';
尝试让jQuery让你的生活变得简单。你可以得到这样的价值观:
$('#cat_ul input[type=radio]:checked').val();
(假设您的&lt; li&gt;标记包含在&lt; ul id =“cat_ul”&gt;标记中)
答案 3 :(得分:0)
除了marvinlabs所说的,尝试使用chrome开发工具来查看ajax返回的代码发生了什么。您将看到确切的ajax responseText以及发送到服务器的Headers的内容。这也包括该领域及其价值。
答案 4 :(得分:0)
假设您要继续不使用jQuery,请将代码更改为以下内容:
function getCheckedValue(radioObjArr) {
if(!radioObjArr)
return "";
for (var ii = 0; ii < radioObjArr.length; i++) {
if (radioObjArr[ii].checked) {
return radioObjArr[ii].value;
}
}
return "";
}
和
function get(obj) {
var poststr ="cat_title=" + escape(encodeURI(document.getElementById("cat_title").value )) +
"&cat_description=" + escape(encodeURI( document.getElementById("cat_description").value ))+
"&cat_id=" + escape(encodeURI( getCheckedValue(document.getElementsByName("cat_id")) ));
makePOSTRequest('categorie.php', poststr);
}
但是目前的代码还不清楚。您在单选按钮上使用ID(“cat_id”),但是您的getCheckedValue函数也支持获取对象数组。但是,ID必须是唯一的。 我稍微改变了你的代码,所以你总是会使用一个对象数组,即使它只有一个元素。