2013-08-29 83 views
-1

我试图用jQuery来定位id。不幸的是,它们嵌套在div中,我的jquery将无法工作。作为一个例子,我将如何定位:jQuery - 如何定位嵌套元素?

id="beds" 

<div id="content"> 
    <div class="page"> 
    <form> 
     <select class="customSelect_beds" id="beds"> 

jQuery的

$(document).ready(function() { 

$('#beds').change(multiply); 
$('#baths').change(multiply); 
$('#frequency').change(multiply); 

function multiply() { 
    var beds = parseFloat($('#beds').val()), 
     baths = parseFloat($('#baths').val()), 
     baselight = 54, 
     bedmodlight = (beds * 12), 
     bathmodlight = (baths * 6), 
     basereg = 54, 
     bedmodreg = (beds * 18), 
     bathmodreg = (baths * 12), 
     basedeep = 72, 
     bedmoddeep = (beds * 24), 
     bathmoddeep = (baths * 18), 
     discount = $('#frequency').val(), 
     light_cleaning = Math.ceil((baselight + bedmodlight + bathmodlight) * discount), 
     regular_cleaning = Math.ceil((basereg + bedmodreg + bathmodreg) * discount), 
     deep_cleaning = Math.ceil((basedeep + bedmoddeep + bathmoddeep) * discount); 

    $('#total_light').text((light_cleaning) || "--"); 
    $('#total_regular').text((regular_cleaning) || "--"); 
    $('#total_deep').text((deep_cleaning) || "--"); 
}; 

}); 我似乎无法在任何地方找到此信息(虽然我仍在寻找)。有小费吗?

编辑 - 代码添加和截断的空间。

+0

不知道为什么我得到downvoted不知道东西.. – user2635811

回答

1

既然你有一个id,你可以使用id-selector

$('#beds') 
+0

这是我目前正在使用,它不起作用。 – user2635811

+0

那么,我的原始方法其实是正确的。我的html在脚本中使用的一个ID中有一个错字。所以这将与深度嵌套的元素一起工作。抱歉怀疑你。 – user2635811

1

试试这个:

$('#content').find('#beds'); 
+0

这似乎并没有在这之后使用代码工作 – user2635811

+0

$('#content')。find('#beds')。change(multiply); – user2635811

+0

什么是(乘)?显示代码 – Anton