jquery语法帮助

时间:2011-03-17 15:14:29

标签: jquery syntax

嘿所有,
我是处理变量的新手,我试图让这个功能正常工作。我想知道是否有人可以帮我找到问题。它似乎根本不起作用。我不确定我是否已正确标记所有内容。

<script type="text/javascript">
    $(document).ready(function() {
        $('.partners_internal_product_select input[type=radio]').live('change', function(){ //Setting up the chenge event for the radio button
            var AffectedRegistration = $(this).closest('.partners_internal_product_registration'); //Select the nearest div with this class
            var ID = $(this).attr('id'); //getting the ID of the radio button
            var IDArray = ID.split('_'); //seperating the ID into an array
            var regURL = 'accomodationpartners/'+IDArray[1]+'.php'; //building a url to load
            $(AffectedRegistration).load(regURL); //loading the url into the nearest div.partners_internal_product_registration
        });
    });

    </script>
    <div class="partners_internal_item_hdr"><h1 class="white">Program Registration</h1></div>
    <div class="partners_internal_item">
        <div class="partners_internal_item_subhdrfull"><h2>Select a Product to Register For:</h2></div>
        <div class="partners_internal_product_select">
            <fieldset><input type="radio" class="productselect" name="productselect" id="productselect_daytrip" /><label for="productselect_daytrip">Day Trip</label></fieldset>
            <fieldset><input type="radio" class="productselect" name="productselect" id="productselect_courseregistraion" /><label for="productselect_courseregistraion">Course Registration</label></fieldset>
            <fieldset><input type="radio" class="productselect" name="productselect" id="productselect_socialbusinessgroup" /><label for="productselect_socialbusinessgroup">Social/Business Group</label></fieldset>
            <fieldset><input type="radio" class="productselect" name="productselect" id="productselect_paddlefest" /><label for="productselect_paddlefest">Paddlefest</label></fieldset>
            <fieldset><input type="radio" class="productselect" name="productselect" id="productselect_paddleplay" /><label for="productselect_paddleplay">Paddle and Play</label></fieldset>
        </div>
        <div class="partners_internal_product_registration"></div>
    </div>

2 个答案:

答案 0 :(得分:3)

jQuery中的closest函数将使用您提供的过滤器查找下一个元素,该过滤器是父元素或父元素的父元素,基本上在DOM树中。

在您的脚本中,您正在从单选按钮中搜索partners_internal_product_registration类,但具有类partners_internal_product_registration的div实际上是单选按钮父级的sibling

你真的需要

var AffectedRegistration = $(this).closest('.partners_internal_product_select')
                                .siblings('partners_internal_product_registration');

答案 1 :(得分:2)

我只能猜测你想用这个做什么,但我怀疑问题出在这一行:

var AffectedRegistration = 
  $(this).closest('.partners_internal_product_registration');

closest()尝试获取具有指定参数的父级,尽管.partners_internal_product_registration div不是radiobutton的父级。

您希望得到最接近的.partners_internal_item,并在其中获取其中的.partners_internal_product_registration div。

您可以使用以下行(使用jQuery context)执行此操作:

var AffectedRegistration = 
   $('.partners_internal_product_registration',  
     $(this).closest('.partners_internal_item'));

或者您可以使用.children()功能:

var AffectedRegistration = 
   $(this).closest('.partners_internal_item')
     .children('.partners_internal_product_registration');