开发者

Can't send variable value into div

开发者 https://www.devze.com 2023-03-07 03:49 出处:网络
I have some variable: <input type=\"text\" id=\"use\" /> <div id=\"app\"></div> I want if ajax success i want to send $(\'#use\').val() into <div>.

I have some variable:

<input type="text" id="use" />
<div id="app"></div>

I want if ajax success i want to send $('#use').val() into <div>. I have try like below:

 success: function(){
                      $("#app").val($("#use").val());
                      }

then:

$("#app").text($("#use").val());

and:

$("#app").replacewith($("#use").val());

But, i get no result. Could you tell me how to do this?


$("#enter").click(function(){
     if($("#passlog").valid()){
     var params = $("#passlog").serialize();
     $.ajax({
              type:"post",
              url:"process3.php",
              data:params,
              cache:false,
              async:false,
              success: function(res){
                            switch(parseInt(res)){
                                   case "1"开发者_StackOverflow:     //i get value 1 from server side if get 1 do the job below
                                           $("#app").text($("#use").val());
                                           break;
                                           }
                             },
              });
         }
    });


Divs do not have values, instead you set their HTML:

$("#app").html($("#use").val());

Or their text:

$("#app").text($("#use").val());

It's also worth nothing though that allowing someone to directly enter code into the DOM of your website is a very bad idea.


I noticed you have an extra comma after the success call on your AJAX, try removing it as that could be causing an issue.

If text() is not working you may have an issue with your AJAX. Do you have an error handler? If not try adding one like the below, and let us know what you get back.

$.ajax({
    type:"post",
    url:"process3.php",
    data:params,
    cache:false,
    async:false,
    success: function(res){
        switch(parseInt(res)){
            case "1":     //i get value 1 from server side if get 1 do the job below
                $("#app").text($("#use").val());
                break;
        }
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(thrownError);
    }
});

}


Your version of

$("#app").text($("#use").val());

should work.

Are you sure there is a value in the input box ?

demo at http://jsfiddle.net/gaby/4cjgA/

(make sure to type something in the #use box before calling the ajax)


Update

Please change the success callback to this, so we can see if the code in the switch is executed.

          success: function(res){
                        switch(parseInt(res)){
                               case "1":     //i get value 1 from server side if get 1 do the job below
                                       alert('1 was returned !');
                                       $("#app").text($("#use").val());
                                       break;
                               default:
                                       alert('Ajax result was not caught in the switch\nReturned data:' + res );
                         }
                   }


success: function(){
    $("#app").val($("#use").val());
 }

this is wrong as you can't set the value of a div. Use text() or html() instead:

success: function(){
    $("#app").text($("#use").val());
}


couple of thoughts--- close your tags

<input type="text" id="use" />
<div id="app" />

also divs don't have value properties, either set an attribute or if you're trying to set the html then use the html method

$("#app").html($("#use).val());
or
$("#app").attr("value", $("#use").val());

also, something is bothering me about having an id of "use", but I think that's just me. I'm always concerned of anything which looks like a keyword.

0

精彩评论

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