在jQuery中,我想編寫一個簡單的函數來顯示警告,如果在選擇框內選擇的值之和的差值> 0,對於同一類的每個div。嵌套div內選擇選項的jQuery計數值
這是我的html代碼:
$(".Y").each(function(index) {
\t var positive = $(this).children('.Z').val();
\t var negative = 0;
\t $(this).children('.T').each(function(index) {
\t \t negative = negative + $(this).val();
\t });
\t
\t var difference = positive - negative;
\t if (difference > 0) {
\t \t alert("Show something!");
\t }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="X">
\t <div class="Y">
\t \t <select class="Z">
\t \t \t <option value="0">0</option>
\t \t \t <option value="1">1</option>
\t \t </select>
\t \t <select class="T">
\t \t \t <option value="0">0</option>
\t \t \t <option value="1">1</option>
\t \t </select>
\t \t <select class="T">
\t \t \t <option value="0">0</option>
\t \t \t <option value="1">1</option>
\t \t </select>
\t <div>
\t <div class="Y">
\t \t <select class="Z">
\t \t \t <option value="0">0</option>
\t \t \t <option value="1">1</option>
\t \t </select>
\t \t <select class="T">
\t \t \t <option value="0">0</option>
\t \t \t <option value="1">1</option>
\t \t </select>
\t \t <select class="T">
\t \t \t <option value="0">0</option>
\t \t \t <option value="1">1</option>
\t \t </select>
<div>
<div>
我的代碼無法正常工作。我錯在哪裏?一些提示?
您對我們有錯誤信息嗎? – manuzi1
嘗試.find('option:selected')。attr('value')而不是.val() – twicejr
@twicejr:沒有理由這麼做。這不是'val'壞了。 –