search

Home  >  Q&A  >  body text

javascript - jquery how to append value to input value

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
</head>
<style type="text/css">
    .box{width: 600px;height: 300px;border: 1px solid #ccc;margin: 0 auto}
    .func span{margin-right: 10px;cursor: pointer;}
</style>
<body>
        <p class="box">
            <input type="text" name="" value="">
        </p>
        <p class="func">
            <span>补水,</span><span>保湿,</span><span>去皱,</span><span>美白,</span>
        </p>
</body>
<script type="text/javascript">
    $(".func span").click(function(){
        h=$(this).html();
        $(".box input").attr("value").append(h);
    });
</script>
</html>

What I wrote here is wrong. I want to click span and then add a value to the input value

漂亮男人漂亮男人2695 days ago1649

reply all(5)I'll reply

  • 学习ing

    学习ing2017-07-05 11:04:29

    $(".box input").val($(".box input").val() + h)

    reply
    0
  • 三叔

    三叔2017-07-05 11:04:29

    $(".box input").val($(".box input").val()+$(this).text())

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 11:04:29

    I don’t know what you mean by appending? What I understand is that originally val was an empty string'', I clicked on 'Hydration,', and then the strings were concatenated, val became 'Hydrating,', then I clicked on 'Moisturizing,' and appended var It becomes 'hydrating, moisturizing,'

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
    </head>
    <style type="text/css">
        .box{width: 600px;height: 300px;border: 1px solid #ccc;margin: 0 auto}
        .func span{margin-right: 10px;cursor: pointer;}
    </style>
    <body>
            <p class="box">
                <input id= "id_box_input" type="text" name="" value="">
            </p>
            <p class="func">
                <span>补水,</span><span>保湿,</span><span>去皱,</span><span>美白,</span>
            </p>
    </body>
    <script type="text/javascript">
        $(".func span").click(function(){
            h=$(this).text();
            $("#id_box_input").val($("#id_box_input").val + h);
        });
    </script>
    </html>

    reply
    0
  • 怪我咯

    怪我咯2017-07-05 11:04:29

    $(".func span").click(function(){
        var text = $(this).text();
        var $input = $(".box input")
        $input.val($input.val()+text);
    });

    reply
    0
  • typecho

    typecho2017-07-05 11:04:29

    Everything mentioned above is correct. You may not understand the difference between append and direct assignment to val(). Append inserts an element into an existing dom node, such as:

    1. 把<span>补水,</span>  插入到p中
    var span = '<span>补水,</span>';
    $("p").append(span),此时span就被加入到p中了
    2. 而赋值直接是在input中给定一个值
    直接
    $("input").val(你需要插入的值)
    
    可以试试看
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>时间</title>
      <style type="text/css">
        .box{width: 600px;height: 300px;border: 1px solid #ccc;margin: 0 auto}
        .func span{margin-right: 10px;cursor: pointer;}
    </style>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
    <body>
            <p class="box" id="p">
                <input type="text" name="" value="">
            </p>
            <p class="func">
                <span>补水,</span><span>保湿,</span><span>去皱,</span><span>美白,</span>
            </p>
    </body>
    <script type="text/javascript">
        $(".func span").click(function(){
            h = $(this).html();
            $(".box input").val($(".box input").val() + h); // val
            $("#p").append(h); // append
        });
    </script>
    </html>

    reply
    0
  • Cancelreply