HTML value of HTML form in loadable AJAX div

I can not get the value of the radio in the form loaded by AJAX in the div. Here is the JavaScript code I'm using: (the radio name is the 'add_category' in the 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 />';
+3
source share
5 answers

I assume the problem is how you catch the radio object in the DOM ....

Please also write a code on how to make a function call getCheckedValue.

Try it in jquery mode ...

$('#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;
}
+1
source

You may need to read the following manuals:

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
+1
source

, , :

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

( <li> < ul id = "cat_ul" > )

0

, marvinlabs, chrome, , ajax. , ajax responseText, , . .

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

. (cat_id) , getCheckedValue . . , , .

0

Source: https://habr.com/ru/post/1761634/


All Articles