weixin_33720186 2015-09-13 20:01 采纳率: 0%
浏览 16

jQuery和高级javascript

i am trying to get a value in input box automatically by clicking button "get name", from database. how can i implement it...?

my so far work is below...

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){


        var n = $("#div2").load("getTotal.html #p1").val();
                $("#div2").val(n);
    });
});
</script>
</head>
<body>

<input type="text" id="div2" value=""/>
<button>Get Name</button>

</body>
</html>

and getTotal.html page contents...

<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">John D. Feller</p>

in doing so.... i am getting an error "[object, Object]".

Help me out....with this error and advice me how can i take values from database.

  • 写回答

1条回答 默认 最新

  • weixin_33728708 2015-09-13 21:08
    关注

    Issues in the code:

    1) $("#div2").load(...) doesn't make much sense because it's telling jQuery to pull the content of the URL as HTML content of #div2. Here #div2 is an input which is not supposed to have HTML content. http://api.jquery.com/load/

    2) You may want to load only the content of #p1 by using getTotal.html #p1, but this syntax won't work as you expected. jQuery can only pull the full content of getTotal.html, then you need to extract content of #p1 from it.

    Assuming getTotal.html page is in same directory as your main page, the JS code can be modified to:

    $(document).ready(function(){
        $("button").click(function(){
            $.get("getTotal.html", null, function(text) {
                $("#div2").val($(text).filter("#p1").html());
            });
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题