大家好,
我是使用变量的新手,并且正在尝试让这个函数正常工作。我想知道是否有人可以帮我找到其中的问题。它似乎根本不起作用。我不确定我是否正确标记了所有内容。
<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>
最佳答案
closest jQuery 中的函数将使用您提供的过滤器查找下一个元素,该元素是父元素或父元素的父元素等,基本上是在 DOM 树中查找。
在您的脚本中,您正在从单选按钮搜索partners_internal_product_registration类,但具有partners_internal_product_registration类的div实际上是sibling单选按钮的父级。
你确实需要
var AffectedRegistration = $(this).closest('.partners_internal_product_select')
.siblings('partners_internal_product_registration');
关于jquery 语法帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5341049/