javascript - jquery how to append value to input value
漂亮男人
漂亮男人 2017-07-05 11:02:29
0
5
1648
<!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

漂亮男人
漂亮男人

reply all(5)
学习ing

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

三叔
$(".box input").val($(".box input").val()+$(this).text())
扔个三星炸死你

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>
刘奇
$(".func span").click(function(){
    var text = $(this).text();
    var $input = $(".box input")
    $input.val($input.val()+text);
});
typecho

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>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template