【发布时间】:2011-08-17 08:30:18
【问题描述】:
我正在制作这个 jQuery 订单表格,但我遇到了一些挑战,因为我对 JavaScript 还很陌生。
我的目标:当有人选择辅助字段时,价格将显示在 class="item-price" 输入字段中。例如:有人点击“8x10”,然后点击“No Frame”,No Frame 的价格将显示在“item-price”字段中。
我将这些字段放在另一个字段的选定字段中。下面是我的 HTML 和 JavaScript:
<form action="" method="post" class="form" name="form">
<div class="selection">
<div class="selection-announcement">Select Your Size</div>
<div class="selection-announcement-hidden">Select Your Frame</div>
<div class="selection-announcement-hidden2">Select Your Giclee Type</div>
</div>
<div class="input" id="8x10">
<label>
<input class="unselected" name="selectiongroup" type="radio" value="8x10" />
<h2>8x10</h2>
</label>
<div><span></span></div>
</div>
<div class="input" id="12x16">
<label>
<input class="unselected" name="selectiongroup" type="radio" value="12x16" />
<h2>12x16</h2>
</label>
<div><span></span></div>
</div>
<div class="input" id="16x24">
<label>
<input class="unselected" name="selectiongroup" type="radio" value="16x24" />
<h2>16x24</h2>
</label>
<div><span></span></div>
</div>
<div class="input" id="12x16-giclee">
<label>
<input class="unselected" name="selectiongroup" type="radio" value="12x16 Giclee" />
<h2>12x16 Giclee</h2>
</label>
<div><span></span></div>
</div>
<div class="input" id="16x24-giclee">
<label>
<input class="unselected" name="selectiongroup" type="radio" value="12x16 Giclee" />
<h2>16x24 Giclee</h2>
</label>
<div><span></span></div>
</div>
<div class="input" id="18x36-giclee">
<label>
<input class="unselected" name="selectiongroup" type="radio" value="12x16 Giclee" />
<h2>18x36 Giclee</h2>
</label>
<div><span></span></div>
</div>
<input name="item-name" type="text" value="" class="item-name" />
<input name="item-price" type="text" value="" class="item-price" />
<input name="Next Step" type="submit" value="Next Step" class="next-step" />
<div class="clear"></div>
</form>
jQuery:
$(document).ready(function(){
var $class = ".input"; // MAIN DIV CLASS
var $1 = "#8x10"; // FIRST ID
var $2 = "#12x16"; // SECOND ID
var $3 = "#16x24"; // THIRD ID
var $4 = "#12x16-giclee"; // 4TH ID
var $5 = "#16x24-giclee"; // 5TH ID
var $6 = "#18x36-giclee"; // 6TH ID
var $heightA = "130"; //px FOR REGULAR PRINTS
var $heightB = "110"; //px FOR GICLEE PRINTS
var $span = "<span></span>"; // ELEMENT TO HOLD HIDDEN FORM FIELDS
var $a1 = ".selection-announcement"; // FIRST ANNOUNCEMENT ID
var $a2 = ".selection-announcement-hidden"; // SECOND ANNOUNCEMENT ID
var $a3 = ".selection-announcement-hidden2"; // THIRD ANNOUNCEMENT ID
// HIDDEN INPUT FIELDS
var $8x10 = "<div><label><input class='test1' id='1' name='selectiongroup2' type='radio' value='No Frame' /><h2>No Frame ($19.95)</h2><h3>Shipping, Handling and Insurance: $14.95</h3></label><label><input class='unselected' id='2' name='selectiongroup2' type='radio' value='Metal Frame' /><h2>Metal Frame ($55.95)</h2><h3>Shipping, Handling and Insurance: $29.95</h3></label><label><input class='unselected' id='3' name='selectiongroup2' type='radio' value='Barnwood Frame' /><h2>Barnwood Frame ($169.95)</h2><h3>Shipping, Handling and Insurance: $49.95</h3></label></div>";
var $12x16 = "<div><label><input class='unselected' id='4' name='selectiongroup2' type='radio' value='No Frame' /><h2>No Frame ($29.95)</h2><h3>Shipping, Handling and Insurance: $14.95</h3></label><label><input class='unselected' id='5' name='selectiongroup2' type='radio' value='Metal Frame' /><h2>Metal Frame ($89.95)</h2><h3>Shipping, Handling and Insurance: $29.95</h3></label><label><input class='unselected' id='6' name='selectiongroup2' type='radio' value='Barnwood Frame' /><h2>Barnwood Frame ($169.95)</h2><h3>Shipping, Handling and Insurance: $49.95</h3></label></div>";
var $16x24 = "<div><label><input class='unselected' id='7' name='selectiongroup2' type='radio' value='No Frame' /><h2>No Frame ($49.95)</h2><h3>Shipping, Handling and Insurance: $14.95</h3></label><label><input class='unselected' id='8' name='selectiongroup2' type='radio' value='Metal Frame' /><h2>Metal Frame ($119.95)</h2><h3>Shipping, Handling and Insurance: $29.95</h3></label><label><input class='unselected' id='9' name='selectiongroup2' type='radio' value='Barnwood Frame' /><h2>Barnwood Frame ($169.95)</h2><h3>Shipping, Handling and Insurance: $49.95</h3></label></div>";
var $12x16G = "<div><label><input class='unselected' id='10' name='selectiongroup2' type='radio' value='Stretched Gallery-Wrapped' /><h2>2' Stretched Gallery-Wrapped ($129.95)</h2><h3>Shipping, Handling and Insurance: $39.95</h3></label><label><input class='unselected' id='11' name='selectiongroup2' type='radio' value='Watercolor' /><h2>Watercolor ($99.95)</h2></label><div><label><input name='With Barnwood Frame' type='checkbox' value='With Barnwood Frame'><h2>With Barnwood Frame ($139.95)</h2><h3>Shipping, Handling and Insurance: $49.95</h3></label></div></div>";
var $16x24G = "<div><label><input class='unselected' id='12' name='selectiongroup2' type='radio' value='Stretched Gallery-Wrapped' /><h2>2' Stretched Gallery-Wrapped ($189.95)</h2><h3>Shipping, Handling and Insurance: $39.95</h3></label><label><input class='unselected' id='13' name='selectiongroup2' type='radio' value='Watercolor' /><h2>Watercolor ($139.95)</h2></label><div><label><input name='With Barnwood Frame' type='checkbox' value='With Barnwood Frame'><h2>With Barnwood Frame ($189.95)</h2><h3>Shipping, Handling and Insurance: $49.95</h3></label></div></div>";
var $18x36G = "<div><label><input class='unselected' id='14' name='selectiongroup2' type='radio' value='Stretched Gallery-Wrapped' /><h2>2' Stretched Gallery-Wrapped ($249.95)</h2><h3>Shipping, Handling and Insurance: $39.95</h3></label><label><input class='unselected' id='15' name='selectiongroup2' type='radio' value='Watercolor' /><h2>Watercolor ($199.95)</h2></label><div><label><input name='With Barnwood Frame' type='checkbox' value='With Barnwood Frame'><h2>With Barnwood Frame ($249.95)</h2><h3>Shipping, Handling and Insurance: $49.95</h3></label></div></div>";
/* FIRST STAGE */
$($1+' input').click(function() {$($1+' input').removeClass('unselected');$($1+' input').addClass('selected');});
$($2+' input').click(function() {$($1+' input').removeClass('selected');$($1+' input').addClass('unselected');});
$($3+' input').click(function() {$($1+' input').removeClass('selected');$($1+' input').addClass('unselected');});
$($4+' input').click(function() {$($1+' input').removeClass('selected');$($1+' input').addClass('unselected');});
$($5+' input').click(function() {$($1+' input').removeClass('selected');$($1+' input').addClass('unselected');});
$($6+' input').click(function() {$($1+' input').removeClass('selected');$($1+' input').addClass('unselected');});
$($2+' input').click(function() {$($2+' input').removeClass('unselected');$($2+' input').addClass('selected');});
$($1+' input').click(function() {$($2+' input').removeClass('selected');$($2+' input').addClass('unselected');});
$($3+' input').click(function() {$($2+' input').removeClass('selected');$($2+' input').addClass('unselected');});
$($4+' input').click(function() {$($2+' input').removeClass('selected');$($2+' input').addClass('unselected');});
$($5+' input').click(function() {$($2+' input').removeClass('selected');$($2+' input').addClass('unselected');});
$($6+' input').click(function() {$($2+' input').removeClass('selected');$($2+' input').addClass('unselected');});
$($3+' input').click(function() {$($3+' input').removeClass('unselected');$($3+' input').addClass('selected');});
$($1+' input').click(function() {$($3+' input').removeClass('selected');$($3+' input').addClass('unselected');});
$($2+' input').click(function() {$($3+' input').removeClass('selected');$($3+' input').addClass('unselected');});
$($4+' input').click(function() {$($3+' input').removeClass('selected');$($3+' input').addClass('unselected');});
$($5+' input').click(function() {$($3+' input').removeClass('selected');$($3+' input').addClass('unselected');});
$($6+' input').click(function() {$($3+' input').removeClass('selected');$($3+' input').addClass('unselected');});
$($4+' input').click(function() {$($4+' input').removeClass('unselected');$($4+' input').addClass('selected');});
$($1+' input').click(function() {$($4+' input').removeClass('selected');$($4+' input').addClass('unselected');});
$($2+' input').click(function() {$($4+' input').removeClass('selected');$($4+' input').addClass('unselected');});
$($3+' input').click(function() {$($4+' input').removeClass('selected');$($4+' input').addClass('unselected');});
$($5+' input').click(function() {$($4+' input').removeClass('selected');$($4+' input').addClass('unselected');});
$($6+' input').click(function() {$($4+' input').removeClass('selected');$($4+' input').addClass('unselected');});
$($5+' input').click(function() {$($5+' input').removeClass('unselected');$($5+' input').addClass('selected');});
$($1+' input').click(function() {$($5+' input').removeClass('selected');$($5+' input').addClass('unselected');});
$($2+' input').click(function() {$($5+' input').removeClass('selected');$($5+' input').addClass('unselected');});
$($3+' input').click(function() {$($5+' input').removeClass('selected');$($5+' input').addClass('unselected');});
$($4+' input').click(function() {$($5+' input').removeClass('selected');$($5+' input').addClass('unselected');});
$($6+' input').click(function() {$($5+' input').removeClass('selected');$($5+' input').addClass('unselected');});
$($6+' input').click(function() {$($6+' input').removeClass('unselected');$($6+' input').addClass('selected');});
$($1+' input').click(function() {$($6+' input').removeClass('selected');$($6+' input').addClass('unselected');});
$($2+' input').click(function() {$($6+' input').removeClass('selected');$($6+' input').addClass('unselected');});
$($3+' input').click(function() {$($6+' input').removeClass('selected');$($6+' input').addClass('unselected');});
$($4+' input').click(function() {$($6+' input').removeClass('selected');$($6+' input').addClass('unselected');});
$($5+' input').click(function() {$($6+' input').removeClass('selected');$($6+' input').addClass('unselected');});
/* SECOND STAGE */
$($1+' input').click(function() {$($1).addClass('input-hover');$($1+' div').animate({height: $heightA},800);});
$($2+' input').click(function() {$($1).removeClass('input-hover');$($1+' div').animate({height: 0},800);});
$($3+' input').click(function() {$($1).removeClass('input-hover');$($1+' div').animate({height: 0},800);});
$($4+' input').click(function() {$($1).removeClass('input-hover');$($1+' div').animate({height: 0},800);});
$($5+' input').click(function() {$($1).removeClass('input-hover');$($1+' div').animate({height: 0},800);});
$($6+' input').click(function() {$($1).removeClass('input-hover');$($1+' div').animate({height: 0},800);});
$($2+' input').click(function() {$($2).addClass('input-hover');$($2+' div').animate({height: $heightA},800);});
$($1+' input').click(function() {$($2).removeClass('input-hover');$($2+' div').animate({height: 0},800);});
$($3+' input').click(function() {$($2).removeClass('input-hover');$($2+' div').animate({height: 0},800);});
$($4+' input').click(function() {$($2).removeClass('input-hover');$($2+' div').animate({height: 0},800);});
$($5+' input').click(function() {$($2).removeClass('input-hover');$($2+' div').animate({height: 0},800);});
$($6+' input').click(function() {$($2).removeClass('input-hover');$($2+' div').animate({height: 0},800);});
$($3+' input').click(function() {$($3).addClass('input-hover');$($3+' div').animate({height: $heightA},800);});
$($1+' input').click(function() {$($3).removeClass('input-hover');$($3+' div').animate({height: 0},800);});
$($2+' input').click(function() {$($3).removeClass('input-hover');$($3+' div').animate({height: 0},800);});
$($4+' input').click(function() {$($3).removeClass('input-hover');$($3+' div').animate({height: 0},800);});
$($5+' input').click(function() {$($3).removeClass('input-hover');$($3+' div').animate({height: 0},800);});
$($6+' input').click(function() {$($3).removeClass('input-hover');$($3+' div').animate({height: 0},800);});
$($4+' input').click(function() {$($4).addClass('input-hover');$($4+' div').animate({height: $heightB},800);});
$($1+' input').click(function() {$($4).removeClass('input-hover');$($4+' div').animate({height: 0},800);});
$($2+' input').click(function() {$($4).removeClass('input-hover');$($4+' div').animate({height: 0},800);});
$($3+' input').click(function() {$($4).removeClass('input-hover');$($4+' div').animate({height: 0},800);});
$($5+' input').click(function() {$($4).removeClass('input-hover');$($4+' div').animate({height: 0},800);});
$($6+' input').click(function() {$($4).removeClass('input-hover');$($4+' div').animate({height: 0},800);});
$($5+' input').click(function() {$($5).addClass('input-hover');$($5+' div').animate({height: $heightB},800);});
$($1+' input').click(function() {$($5).removeClass('input-hover');$($5+' div').animate({height: 0},800);});
$($2+' input').click(function() {$($5).removeClass('input-hover');$($5+' div').animate({height: 0},800);});
$($3+' input').click(function() {$($5).removeClass('input-hover');$($5+' div').animate({height: 0},800);});
$($4+' input').click(function() {$($5).removeClass('input-hover');$($5+' div').animate({height: 0},800);});
$($6+' input').click(function() {$($5).removeClass('input-hover');$($5+' div').animate({height: 0},800);});
$($6+' input').click(function() {$($6).addClass('input-hover');$($6+' div').animate({height: $heightB},800);});
$($1+' input').click(function() {$($6).removeClass('input-hover');$($6+' div').animate({height: 0},800);});
$($2+' input').click(function() {$($6).removeClass('input-hover');$($6+' div').animate({height: 0},800);});
$($3+' input').click(function() {$($6).removeClass('input-hover');$($6+' div').animate({height: 0},800);});
$($4+' input').click(function() {$($6).removeClass('input-hover');$($6+' div').animate({height: 0},800);});
$($5+' input').click(function() {$($6).removeClass('input-hover');$($6+' div').animate({height: 0},800);});
/* THIRD STAGE */
$($1+' input').click(function() {$($1+' div span').replaceWith($8x10);})
$($2+' input').click(function() {$($1+' div div').replaceWith($span);});
$($3+' input').click(function() {$($1+' div div').replaceWith($span);});
$($4+' input').click(function() {$($1+' div div').replaceWith($span);});
$($5+' input').click(function() {$($1+' div div').replaceWith($span);});
$($6+' input').click(function() {$($1+' div div').replaceWith($span);});
$($2+' input').click(function() {$($2+' div span').replaceWith($12x16);})
$($1+' input').click(function() {$($2+' div div').replaceWith($span);});
$($3+' input').click(function() {$($2+' div div').replaceWith($span);});
$($4+' input').click(function() {$($2+' div div').replaceWith($span);});
$($5+' input').click(function() {$($2+' div div').replaceWith($span);});
$($6+' input').click(function() {$($2+' div div').replaceWith($span);});
$($3+' input').click(function() {$($3+' div span').replaceWith($16x24);})
$($1+' input').click(function() {$($3+' div div').replaceWith($span);});
$($2+' input').click(function() {$($3+' div div').replaceWith($span);});
$($4+' input').click(function() {$($3+' div div').replaceWith($span);});
$($5+' input').click(function() {$($3+' div div').replaceWith($span);});
$($6+' input').click(function() {$($3+' div div').replaceWith($span);});
$($4+' input').click(function() {$($4+' div span').replaceWith($12x16G);})
$($1+' input').click(function() {$($4+' div div').replaceWith($span);});
$($2+' input').click(function() {$($4+' div div').replaceWith($span);});
$($3+' input').click(function() {$($4+' div div').replaceWith($span);});
$($5+' input').click(function() {$($4+' div div').replaceWith($span);});
$($6+' input').click(function() {$($4+' div div').replaceWith($span);});
$($5+' input').click(function() {$($5+' div span').replaceWith($16x24G);})
$($1+' input').click(function() {$($5+' div div').replaceWith($span);});
$($2+' input').click(function() {$($5+' div div').replaceWith($span);});
$($3+' input').click(function() {$($5+' div div').replaceWith($span);});
$($4+' input').click(function() {$($5+' div div').replaceWith($span);});
$($6+' input').click(function() {$($5+' div div').replaceWith($span);});
$($6+' input').click(function() {$($6+' div span').replaceWith($18x36G);})
$($1+' input').click(function() {$($6+' div div').replaceWith($span);});
$($2+' input').click(function() {$($6+' div div').replaceWith($span);});
$($3+' input').click(function() {$($6+' div div').replaceWith($span);});
$($4+' input').click(function() {$($6+' div div').replaceWith($span);});
$($5+' input').click(function() {$($6+' div div').replaceWith($span);});
/* ANNOUNCEMENT CHANGE */
$($1+' input').click(function() {$($a1).fadeOut("slow"); $($a3).fadeOut("slow"); $($a2).fadeIn("slow");});
$($2+' input').click(function() {$($a1).fadeOut("slow"); $($a3).fadeOut("slow"); $($a2).fadeIn("slow");});
$($3+' input').click(function() {$($a1).fadeOut("slow"); $($a3).fadeOut("slow"); $($a2).fadeIn("slow");});
$($4+' input').click(function() {$($a1).fadeOut("slow"); $($a2).fadeOut("slow"); $($a3).fadeIn("slow");});
$($5+' input').click(function() {$($a1).fadeOut("slow"); $($a2).fadeOut("slow"); $($a3).fadeIn("slow");});
$($6+' input').click(function() {$($a1).fadeOut("slow"); $($a2).fadeOut("slow"); $($a3).fadeIn("slow");});
/* GET THE VALUES TO THE NEXT STAGE */
$('.test1').click(function() {$('.item-price').replaceWith('<input name="item-price" type="text" value="34.90" class="item-price" />');});
});
请帮忙。如果您需要回答更多问题来帮助我解决这个问题,请告诉我。
【问题讨论】:
-
@user713052:价格是唯一显示在动态添加的 HTML 中的地方吗?有什么办法可以将它放在
data-*属性上?另外,ids 不允许以数字开头。 -
实际的“item-price”输入字段不是动态输入的。只有辅助选择。 jQuery 是否可以在动态添加的 HTML 被添加后使用它?
-
@user713052:是的,这是可能的,但可能有更简单的方法。我想我的意思是——有没有办法让你的 HTML 中的项目与价格相关联?另外,您是否有理由不能隐藏
inputs 的子级别并只显示它们而不是将内容插入页面?