使用jQuery从按钮传递正确的ID号

时间:2016-07-15 14:42:44

标签: javascript jquery

我试图在页面上的这个按钮被点击后获得正确的身份证号码,我坚持在这里获得正确传递的正确身份证号码。

我有一个测试代码,显示我想要做的事情:

python -c 'import sys; map(sys.stdout.write, map(" ".join, sys.stdin))' in.txt > new.txt

2 个答案:

答案 0 :(得分:0)

  1. 包裹div中的所有项目,例如

    <div class="myappLink">
        <center><p style="color:#ffffff;font-weight:normal;font-size: 12px;" id="box_1"> 12 meter </p></center>
        <center><p><a data-text="Welcome" data-link="http://www.test.com" class="myapp app_btn_small" id="range-logo">.</a></p></center>
    </div>
    
  2. 添加以下行以获取ID

      

    var $ id = $(this).parents(“。myappLink”)。find(“p:first”)。attr(“id”);

  3. 这是完整的代码

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE HTML PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <style type="text/css">
            .w3_myapp_btn_small {
                font-size: 12px;
                background-size: 16px;
                background-position: 5px 2px;
                padding: 3px 6px 3px 17px; /*25*/
            }
    
            a.myapp {
                color: #fff;
            }
    
            #range-logo {
                background-image: url('icon.png');
                display: block;
                height: 15px;
                overflow: hidden;
                text-indent: 100%;
                white-space: nowrap;
                width: 5px;
            }
        </style>
        <script type="text/javascript">
    
            $(document).ready(function () {
    
                var isMobile = {
                    Android: function () {
                        return navigator.userAgent.match(/Android/i);
                    },
                    BlackBerry: function () {
                        return navigator.userAgent.match(/BlackBerry/i);
                    },
                    iOS: function () {
                        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                    },
                    Opera: function () {
                        return navigator.userAgent.match(/Opera Mini/i);
                    },
                    Windows: function () {
                        return navigator.userAgent.match(/IEMobile/i);
                    },
                    any: function () {
                        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                    }
                };
    
                $(document).on("click", '.myapp', function () {
    
                    if (isMobile.any()) {
    
                        //var text = $(this).attr("data-text");
                        var url = $(this).attr("data-link");
    
                        // The right ID  number in here
                        //var $wbox = $('#box_').text();
    
                        var $id = $(this).parents(".myappLink").find("p:first").attr("id");
    
                        var $wbox = $('#' + $id).text();
    
                        var message = encodeURIComponent($wbox) + " - " + encodeURIComponent(url);
    
                        var myapp_url = "myapp://send?text=" + message;
                        window.location.href = myapp_url;
    
                    } else {
    
                        alert("mobile devices only");
                    }
    
                });
            });
        </script>
    
    </head>
    <body bgcolor="#1A1A1A">
        <br><br>
        <div class="myappLink">
            <center><p style="color:#ffffff;font-weight:normal;font-size: 12px;" id="box_1"> 12 meter </p></center>
            <center><p><a data-text="Welcome" data-link="http://www.test.com" class="myapp app_btn_small" id="range-logo">.</a></p></center>
        </div>
        <br><br><br>
        <div class="myappLink">
            <center><p style="color:#ffffff;font-weight:normal;font-size: 12px;" id="box_2"> 13 yard </p></center>
            <center><p><a data-text="Welcome" data-link="http://www.test.com" class="myapp app_btn_small" id="range-logo">.</a></p></center>
        </div>
        <br><br><br>
        <div class="myappLink">
            <center><p style="color:#ffffff;font-weight:normal;font-size: 12px;" id="box_3"> 14 lbs </p></center>
            <center><p><a data-text="Welcome" data-link="http://www.test.com" class="myapp app_btn_small" id="range-logo">.</a></p></center>
        </div>
        <br><br><br>
    
    </body>
    </html>

答案 1 :(得分:0)

我假设您正在尝试获取最近框的ID(因为所有a锚点的ID都相同)。

<center><p style="color:#ffffff;font-weight:normal;font-size: 12px;"  id="box_3"> 14 lbs </p></center>
<center><p><a data-text="Welcome" data-link="http://www.test.com" class="myapp app_btn_small" id="range-logo">.</a></p></center>

您获得了锚点($(this)),然后转到center然后.prev(上一个兄弟),然后转到p

var id = $(this).closest("center").prev().find("p").prop("id");   
// eg "box_3"

只有在不更改html结构时才能使用。