尝试将div内容从模板div(o_time_templ)复制到另一个div并替换其中的两个单词。 在更改选择它应该复制的内容,把它放在'o_time_mss',并取代变化1和2.将div内容复制到另一个div并替换单词
我的代码如下。第一个克隆后克隆内容为空。
$('select[id=o_time]').on('change', function() {
var template = $("#o_time_templ p").clone().html();
var delTimeVars = $('#o_time').val();
delTimeVars = delTimeVars.split('-');
var delTime = delTimeVars[0];
var delTime_price = delTimeVars[1];
if (delTime_price != undefined) {
$("#o_time_mss").replaceWith(template);
$("#o_time_mss p").text($("#o_time_mss p").text().replace("o_time_zone_screen", delTime).replace("o_time_price_screen", delTime_price));
$("#o_time_mss").show();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control" name="o_time" id="o_time" required>
<option value="">Delivery time</option>
<option value="Normal lunch">Normal lunch</option>
<option value="Breakfast-20">Breakfast</option>
<option value="After 5pm-40">After 5pm)</option>
<option value="Saturday-120">Saturday</option>
<option value="Sunday-120">Sunday</option>
</select>
<div class="hidden" id="o_time_mss">
<p>change 1: o_time_zone_screen AND change 2: o_time_price_screen</p>
</div>
<div class="hidden" id="o_time_templ">
<p>change 1: o_time_zone_screen AND change 2: o_time_price_screen</p>
</div>
@JamieBarker改变了帖子。我改变了我的解决方案,以保持正确的Q与正确的A.感谢下面的Vegeta。 – KJS
这个问题不应该与答案一起更新,否则对任何有同样问题的人都没有帮助。我所做的只是将代码移入片段。请将其还原,以便我们无需让主持人参与。 –
如果您愿意,我可以用完整的工作解决方案编辑Vegeta的答案。 –