0

PHP変数を取得してjavascriptを介して送信しようとしていますが、問題が発生しています。オンラインで見つけたjQueryポップアップを使用しています。ユーザーがアイテムをクリックしたときに、フォームを追加するポップアップでそのitem_id変数をフェッチできるようにする必要があります。

私のPHPセクション。

<a href="#?w=500&item_id='.$stock['id'].'" rel="popup1" class="poplight">

とJavascript。

$(document).ready(function(){

    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[href^=#]').click(function() {
        var popID = $(this).attr('rel'); //Get Popup Name
        var popURL = $(this).attr('href'); //Get Popup href to define size

        //Pull Query & Variables from href URL
        var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value
        var itemID = dim[0].split('=')[2]; //Gets the second query string value

        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/layout/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

        //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;

        document.getElementById('popup1').innerHtml = itemID;

        //Apply Margin to Popup
        $('#' + popID).css({ 
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });

        //Fade in Background
        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 

        return false;
    });


    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();  
    }); //fade them both out

        return false;
    });


});

私はあなたが働いていると思ったものを追加しましたが、そうではありませvar itemID = dim[0].split('=')[2]; //Gets the second query string valuedocument.getElementById('popup1').innerHtml = itemID;

4

2 に答える 2

0

元のコードには単純な見落としがあります。dim変数を取得すると、 & で分割された 2 つの項目を持つ配列が得られます。上記のコードでは、.["w=500", "item_id=some_value"]したがって、itemID を取得するには、dim[1] を使用する必要があります。 var itemID=dim[1].split('=')[1];

于 2013-02-17T18:48:07.547 に答える