0

ボタンを押した後、項目 (リンク) のリストにチェックボックスを追加しようとしています。

アイデアは、次のようなリンクのリストを取得したことです。

  • Google COM。
  • Bing.com
  • Cnn.com

など、ページの下部にボタンがあり、ボタンをクリックすると、各リンクにチェックボックスを追加したいのですが、各ボックスIDはリンク先のシリアル番号にする必要があります。

$("#links_remove").click(function(event) {


$('.links').after('<div><label> Check box to remove link: <input type="checkbox" class=" removebox" id="'+$(this).attr("id").split("_")[1]+'"></div>');
});

そして、これがHTMLです

<a class="links" id="link_27" href="google.com">google.com</a>

しかしもちろん、$(this) セレクターは、現在のリンクではなく、ボタン ("links_remove") の属性を取得します。

ありがとう!

4

3 に答える 3

3

現在のリンクを指す関数引数をafter()内部に使用できます。this

$("#links_remove").click(function(event) {
    $('.links').after(function(index) {

        // here "this" refers to the current ".link" and not "#links_remove"

        return '<div><label> Check box to remove link: <input type="checkbox" class=" removebox" id="'+this.id.split("_")[1]+'"></div>';
    });
});
于 2013-07-19T10:14:06.367 に答える
0

すべてのリンクを取得するために for-each ループを使用しました: http://jsfiddle.net/ehV9L/

$("#links_remove").click(function (event) {
    $('.links').each(function () {
        $(this).after('<span><label> Check box to remove link: <input type="checkbox" class="removebox" id="' + $(this).attr("id").split("_")[1] + '"></span>');
    });
});
<ul>
    <li><a class="links" id="link_01" href="http://www.google.com">Google</a></li>
    <li><a class="links" id="link_02" href="http://www.bing.com">Bing</a></li>
    <li><a class="links" id="link_03" href="http://www.cnn.com">CNN</a></li>
</ul>
<input type="button" id="links_remove" value="Remove Links" />
于 2013-07-19T10:30:42.390 に答える
0

これで仕事が完了するはずです:

$('.links').each(function(){
    $(this).after('<div><label> Check box to remove link: <input type="checkbox" class=" removebox" id="'+$(this).attr("id").split("_")[1]+'"></div>');
});
于 2013-07-19T10:14:14.627 に答える