美文网首页
jq选中添加类名,及获取数据赋值到input

jq选中添加类名,及获取数据赋值到input

作者: 北上广_d8bd | 来源:发表于2019-06-05 08:12 被阅读0次

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

    <style>

        *{

            margin: 0;

            padding: 0;

            list-style: none;

        }

        ul>li{

            line-height: 2;

            text-align: center;

        }

        .add{

            color:red;

        }

        input{

            width: 300px;

            height: 20px;

            border: solid 1px #ccc;

        }

    </style>

</head>

<body>

    <input id="iptvalue" type="text" placeholder="333">

    <ul class="uls">

        <li>1元</li>

        <li>2元</li>

        <li>3元</li>

        <li>4元</li>

        <li>请输入金额</li>

    </ul>

    <script>

        $('.uls li').click(function(event){

            for(var i=0;i<$('.uls li').length;i++){

                if(i != $(this).index()){

                    $('.uls li')[i].className = ''

                }else if(event.target.innerHTML == '请输入金额'){

                    alert('获取金额')

                }else{

                    $('.uls li')[i].className = 'add'

                    var num = event.target.innerHTML.substring(0,event.target.innerHTML.length-1)

                    $('#iptvalue').val(Number(num).toFixed(2))

                }

            }

        })

    </script>

</body>

</html>

相关文章

网友评论

      本文标题:jq选中添加类名,及获取数据赋值到input

      本文链接:https://www.haomeiwen.com/subject/iyaptctx.html