开发者

How can I display the value using ajax

开发者 https://www.devze.com 2023-02-02 18:58 出处:网络
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.

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(f开发者_JAVA百科unction () {
    $('.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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号