AJAX 加载的 div 中的 HTML 表单单选值
我无法获取由 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 />';
I'm unable to get a radio value in a form loaded by AJAX into a div.
Here is the JavaScript code I'm using:
(the radio name is 'categorie_add' in get function):
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);
}
and I have this in a PHP file:
echo '<li><input type="radio" name="cat_id" id="cat_id" value="' . $value['cat_id'] . '" /> ' . htmlentities($value['cat_title']) . '<br />';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想说问题是你生成单选按钮的方式:
尝试使用 jQuery 让你的生活变得简单。您可以得到这样的值:(
假设您的
标签中)
I would say the problem is the way you generate your radio buttons:
Try to get jQuery to make your life simple. You could get your values like that:
(assuming your <li> tags are wrapped in a <ul id="cat_ul"> tag)
除了marvinlabs所说的之外,尝试使用chrome开发工具看看ajax返回的代码上发生了什么。您将看到确切的 ajax 响应文本以及发送到服务器的标头内容。这还包括字段及其值。
In addition to what marvinlabs says, try to use chrome development tools to see what happening on the code return by ajax. You will see what the exact ajax responseText and also the contents of the Headers sent to the server. This also include the field and its value.
假设您想继续不使用 jQuery,请将代码更改为以下内容:
不过
,您当前的代码还不清楚。您在单选按钮上使用 ID(“cat_id”),但 getCheckedValue 函数还支持获取对象数组。不过,ID 必须是唯一的。
我对您的代码做了一些更改,因此您将始终使用一组对象,即使它只有一个元素。
Assuming you want to keep on not using jQuery, change your code to the following:
And
Your current code is unclear, though. You use an ID ("cat_id") on your radio button, but your getCheckedValue-function also supports getting an array of objects. An ID has to be unique, though.
I changed your code a little so you'll always use an array of objects, even if it has only one element.
您可能需要阅读本教程:
You may need to read this tutorials:
我想问题在于你在 DOM 中捕获无线电对象的方式......
请编写如何调用函数
getCheckedValue
的代码。尝试使用 Jquery 方式...
Jquery 很摇滚。
JavaScript 函数:
I suppose the problem is in the way you catch the radio object in DOM ....
Please write too the code how make the call to the function
getCheckedValue
.Try in Jquery way...
Jquery is Rock.
JavaScript function: