简体   繁体   中英

How can I display the value using ajax

Hi. I need help with this section of code.

What I want to do is; when I click (for example) a radio button 1, that button value display on the browser. I have a three radio buttons.

Here's my ajax query:

function updatepayment(this.value)()
var ajaxResponse = new Object();
  $(document).ready(function () {
    $('.rmr').click(function () {
    var rbVal = $(this).val();
        var myContent;
        if (ajaxResponse[rbVal]) { //in cache
          myContent =  ajaxResponse[rbVal];
          $("#contentcontainer").html(myContent);
        }
        else { // not in cache
      var urlForAjaxCall = "include/function.php" + rbVal + ".html"; 
          $.post(urlForAjaxCall, function (myContent) {
             ajaxResponse[rbVal] = myContent;
             $("#contentcontainer").html(myContent);
          });
        }
      });
    });
  1. Your cache check will always be false since you always re-declare ajaxResponse in the function. ajaxResponse should be declared globally.
  2. And you dont really need the post function (as your not posting any data) you could just use the .load function.
  3. You dont need a function to wrap the $(document).ready function

var ajaxResponse = new Object();

$(document).ready(function(){

 $('.rmr').click(function (){ var rbVal = $(this).val(); var myContent; if (ajaxResponse[rbVal]) { //in cache $("#contentcontainer").html( ajaxResponse[rbVal] ); } else { // not in cache var urlForAjaxCall = "include/function.php" + rbVal + ".html"; $("#contentcontainer").load(urlForAjaxCall, function (myContent) { //Extra processing can be done here, like your cache ajaxResponse[rbVal] = myContent; }); } 

});

});

EDIT: After seeing your comments on your OP You are trying to select the radio buttons by class name "rmr" but do not have it set on the tags you need:

<input class="rmr" type="radio" name="rmr" id="payment1" value="3" onclick="updatepayment(this.value)" /> <br>
<input class="rmr" type="radio" name="rmr" id="payment2" value="5.5" onclick="updatepayment(this.value)" /><br>
<input class="rmr" type="radio" name="rmr" id="payment4" value="10" onclick="updatepayment(this.value)" /><br>

or you could use

$("input[name=rmr]")

to select them by their input name attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM