【发布时间】:2011-04-04 00:51:12
【问题描述】:
我无法将 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 />';
【问题讨论】:
-
你能发布你的“makePOSTRequest”吗?
-
没有足够的上下文来说明您的问题是什么,但其中存在一些潜在的陷阱。看起来您可能正在使用
id="cat_id"在页面上创建多个元素;这是无效的,会混淆getElementById。您应该在插入 URL 段的值上使用encodeURIComponent(),而不是用于整个 URL 的encodeURI(),绝对不是完全伪造的escape(),它永远不应该用于任何事情。首选htmlspecialchars()而不是htmlentities(),这会弄乱非 ISO-8859-1 编码的字符。 -
请注意,如果您使用
someElement.getElementsByName()来获取name="cat_id"输入,而不是老式的form.elementname,您将始终得到一个列表,因此您可以避免检查getCheckedValue()中令人讨厌的单元素特例。 -
这些 cmets 解决了我的问题。非常感谢
标签: javascript html ajax radio-button