AJAX 加载的 div 中的 HTML 表单单选值

发布于 2024-09-16 04:51:25 字数 1101 浏览 4 评论 0原文

我无法获取由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

庆幸我还是我 2024-09-23 04:51:26

我想说问题是你生成单选按钮的方式:

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();

假设您的

  • 标签包含在
      标签中)
  • I would say the problem is the way you generate your radio buttons:

    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 />';
    

    Try to get jQuery to make your life simple. You could get your values like that:

    $('#cat_ul input[type=radio]:checked').val();
    

    (assuming your <li> tags are wrapped in a <ul id="cat_ul"> tag)

    静水深流 2024-09-23 04:51:26

    除了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.

    烟燃烟灭 2024-09-23 04:51:26

    假设您想继续不使用 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 必须是唯一的。
    我对您的代码做了一些更改,因此您将始终使用一组对象,即使它只有一个元素。

    Assuming you want to keep on not using jQuery, change your code to the following:

    function getCheckedValue(radioObjArr) {
      if(!radioObjArr)
        return "";
      for (var ii = 0; ii < radioObjArr.length; i++) {
        if (radioObjArr[ii].checked) {
          return radioObjArr[ii].value;
        }
      }
      return "";
    }
    

    And

    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);
    }
    

    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.

    烟花易冷人易散 2024-09-23 04:51:25

    您可能需要阅读本教程:

    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
    

    You may need to read this tutorials:

    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
    
    还给你自由 2024-09-23 04:51:25

    我想问题在于你在 DOM 中捕获无线电对象的方式......

    请编写如何调用函数 getCheckedValue 的代码。

    尝试使用 Jquery 方式...

    $('#cat_id:checked').val();
    

    Jquery 很摇滚。

    JavaScript 函数:

    function getCheckedValue(radioObj) {
      for ( var i in radioObj )if (radioObj[i].checked) return radioObj[i].value;
      return false;
    }
    

    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...

    $('#cat_id:checked').val();
    

    Jquery is Rock.

    JavaScript function:

    function getCheckedValue(radioObj) {
      for ( var i in radioObj )if (radioObj[i].checked) return radioObj[i].value;
      return false;
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文