问题
I'm using Drupal to create a measurement sheet (Measurement sheets in Drupal 6) and am at a point where I have all the fields in place, I just need to write some jQuery that auto-updates the appropriate field when the user enters data. Code below:
jQuery(document).ready( function ($) {
$('input').change(
function() {
$parent = $(this).parent("td").children("div").children("div");
length = $parent.children("div[id*='field-length-0-wrapper']").children("div.container-inline").children('div[id*="field-length-0-value-wrapper"]').children('input[id*="field-length-0-value"]').val(); // get the width
width = $parent.children("div[id*='field-width-0-wrapper']").children("div.container-inline").children('div[id*="field-width-0-value-wrapper"]').children('input[id*="field-width-0-value"]').val(); // get the length
$parent.children("div[id*='field-area-0-wrapper']").children("div.container-inline").children('div[id*="field-area-0-value-wrapper"]').children('input[id*="field-area-0-value"]').val(length * width); //put area
}
);
});
This interacts with the following form code:
<thead class="tableHeader-processed"><tr><th colspan="2">Measurements: </th> </tr></thead>
<tbody>
<tr class="draggable odd"><td class="content-multiple-drag"><a title="Drag to re-order" href="#" class="tabledrag-handle"><div class="handle"> </div></a></td><td><div class="form-item" id="edit-field-room-0-wrapper">
<div class="form-item" id="edit-field-room-0-value-wrapper">
<div class="form-item" id="edit-field-room-0-value-field-roomname-0-value-wrapper">
<label for="edit-field-room-0-value-field-roomname-0-value">Room: </label>
<input autocomplete="OFF" name="field_room[0][value][field_roomname][0][value]" id="edit-field-room-0-value-field-roomname-0-value" size="50" value="" class="form-text form-autocomplete text" type="text">
</div>
<input class="autocomplete autocomplete-processed" id="edit-field-room-0-value-field-roomname-0-value-autocomplete" value="http://www.imeasure.hairysquid.net/autocomplete_widgets/room/field_roomname" disabled="disabled" type="hidden"><div class="form-item" id="edit-field-room-0-value-field-length-0-wrapper">
<label for="edit-field-room-0-value-field-length-0">Length: </label>
<div class="container-inline"><div class="form-item" id="edit-field-room-0-value-field-length-0-value-wrapper">
<input maxlength="16" name="field_room[0][value][field_length][0][value]" id="edit-field-room-0-value-field-length-0-value" size="24" value="" class="form-text formatted-number formatted-number-processed" decimals="2" type="text">
</div>
<div class="form-item" id="edit-field-room-0-value-field-length-0-unit-wrapper">
<select name="field_room[0][value][field_length][0][unit]" class="form-select" id="edit-field-room-0-value-field-length-0-unit"><option value="" selected="selected">---</option><option value="length_meter">m</option><option value="length_foot">ft</option><option value="length_inch">in</option></select>
</div>
</div>
</div>
<div class="form-item" id="edit-field-room-0-value-field-width-0-wrapper">
<label for="edit-field-room-0-value-field-width-0">Width: </label>
<div class="container-inline"><div class="form-item" id="edit-field-room-0-value-field-width-0-value-wrapper">
<input maxlength="16" name="field_room[0][value][field_width][0][value]" id="edit-field-room-0-value-field-width-0-value" size="24" value="" class="form-text formatted-number formatted-number-processed" decimals="2" type="text">
</div>
<div class="form-item" id="edit-field-room-0-value-field-width-0-unit-wrapper">
<select name="field_room[0][value][field_width][0][unit]" class="form-select" id="edit-field-room-0-value-field-width-0-unit"><option value="" selected="selected">---</option><option value="length_meter">m</option><option value="length_foot">ft</option><option value="length_inch">in</option></select>
</div>
</div>
</div>
<div class="form-item" id="edit-field-room-0-value-field-area-0-wrapper">
<label for="edit-field-room-0-value-field-area-0">Area: </label>
<div class="container-inline"><div class="form-item" id="edit-field-room-0-value-field-area-0-value-wrapper">
<input maxlength="16" name="field_room[0][value][field_area][0][value]" id="edit-field-room-0-value-field-area-0-value" size="24" value="" class="form-text formatted-number formatted-number-processed" decimals="2" type="text">
</div>
<div class="form-item" id="edit-field-room-0-value-field-area-0-unit-wrapper">
<select name="field_room[0][value][field_area][0][unit]" class="form-select" id="edit-field-room-0-value-field-area-0-unit"><option value="" selected="selected">---</option><option value="area_square_foot">ft2</option><option value="area_square_meter">m2</option></select>
</div>
</div>
</div>
</div>
</div>
</td><td style="display: none;" class="delta-order"><div class="form-item" id="edit-field-room-0--weight-wrapper">
<select name="field_room[0][_weight]" class="form-select field_room-delta-order" id="edit-field-room-0--weight"><option value="-2">-2</option><option value="-1">-1</option><option value="0" selected="selected">0</option><option value="1">1</option><option value="2">2</option></select>
</div>
</td> </tr>
<tr class="draggable even"><td class="content-multiple-drag"><a title="Drag to re-order" href="#" class="tabledrag-handle"><div class="handle"> </div></a></td><td><div class="form-item" id="edit-field-room-1-wrapper">
<div class="form-item" id="edit-field-room-1-value-wrapper">
<div class="form-item" id="edit-field-room-1-value-field-roomname-0-value-wrapper">
<label for="edit-field-room-1-value-field-roomname-0-value">Room: </label>
<input autocomplete="OFF" name="field_room[1][value][field_roomname][0][value]" id="edit-field-room-1-value-field-roomname-0-value" size="50" value="" class="form-text form-autocomplete text" type="text">
</div>
<input class="autocomplete autocomplete-processed" id="edit-field-room-1-value-field-roomname-0-value-autocomplete" value="http://www.imeasure.hairysquid.net/autocomplete_widgets/room/field_roomname" disabled="disabled" type="hidden"><div class="form-item" id="edit-field-room-1-value-field-length-0-wrapper">
<label for="edit-field-room-1-value-field-length-0">Length: </label>
<div class="container-inline"><div class="form-item" id="edit-field-room-1-value-field-length-0-value-wrapper">
<input maxlength="16" name="field_room[1][value][field_length][0][value]" id="edit-field-room-1-value-field-length-0-value" size="24" value="" class="form-text formatted-number formatted-number-processed" decimals="2" type="text">
</div>
<div class="form-item" id="edit-field-room-1-value-field-length-0-unit-wrapper">
<select name="field_room[1][value][field_length][0][unit]" class="form-select" id="edit-field-room-1-value-field-length-0-unit"><option value="" selected="selected">---</option><option value="length_meter">m</option><option value="length_foot">ft</option><option value="length_inch">in</option></select>
</div>
</div>
</div>
<div class="form-item" id="edit-field-room-1-value-field-width-0-wrapper">
<label for="edit-field-room-1-value-field-width-0">Width: </label>
<div class="container-inline"><div class="form-item" id="edit-field-room-1-value-field-width-0-value-wrapper">
<input maxlength="16" name="field_room[1][value][field_width][0][value]" id="edit-field-room-1-value-field-width-0-value" size="24" value="" class="form-text formatted-number formatted-number-processed" decimals="2" type="text">
</div>
<div class="form-item" id="edit-field-room-1-value-field-width-0-unit-wrapper">
<select name="field_room[1][value][field_width][0][unit]" class="form-select" id="edit-field-room-1-value-field-width-0-unit"><option value="" selected="selected">---</option><option value="length_meter">m</option><option value="length_foot">ft</option><option value="length_inch">in</option></select>
</div>
</div>
</div>
<div class="form-item" id="edit-field-room-1-value-field-area-0-wrapper">
<label for="edit-field-room-1-value-field-area-0">Area: </label>
<div class="container-inline"><div class="form-item" id="edit-field-room-1-value-field-area-0-value-wrapper">
<input maxlength="16" name="field_room[1][value][field_area][0][value]" id="edit-field-room-1-value-field-area-0-value" size="24" value="" class="form-text formatted-number formatted-number-processed" decimals="2" type="text">
</div>
<div class="form-item" id="edit-field-room-1-value-field-area-0-unit-wrapper">
<select name="field_room[1][value][field_area][0][unit]" class="form-select" id="edit-field-room-1-value-field-area-0-unit"><option value="" selected="selected">---</option><option value="area_square_foot">ft2</option><option value="area_square_meter">m2</option></select>
</div>
</div>
</div>
</div>
</div>
</td><td style="display: none;" class="delta-order"><div class="form-item" id="edit-field-room-1--weight-wrapper">
<select name="field_room[1][_weight]" class="form-select field_room-delta-order" id="edit-field-room-1--weight"><option value="-2">-2</option><option value="-1">-1</option><option value="0">0</option><option value="1" selected="selected">1</option><option value="2">2</option></select>
</div>
</td> </tr>
</tbody>
This displays two room blocks -- notice the field_room[] value. Essentially what I did was create two CCK content types -- Sheet and Room. Room has four text fields: Name, Width, Length, Area -- in that order. Name is an autocomplete field, Width, Length and Area are Measured Value Fields, meaning can switch the unit. Then, in the Sheet content type, I use flexinode to create a repeating series of room content types.
What's wrong with my jQuery?
Any help is appreciated. :)
回答1:
In your jQuery code, there should be parents
used instead of parent
for td
lookup:
$parent = $(this).parents("td").children("div").children("div");
This makes area value to be calculated once width or length is changed.
EDIT:
There is also possible optimization of jQuery, for example to this (can be improved even more):
jQuery(document).ready( function ($) {
$('input').change(function() {
var $parent = $(this).parents("td").children("div").children("div"),
length = $parent.find('input[id*="field-length-0-value"]').val(),
width = $parent.find('input[id*="field-width-0-value"]').val();
$parent.find('input[id*="field-area-0-value"]').val(length * width);
});
});
来源:https://stackoverflow.com/questions/4786716/auto-updating-width-length-area-using-jquery-and-drupal6