我建议:
$('#submit').click(function(e){
// prevents the form's submission
e.preventDefault();
$('selector').text($('#routeStart').val()); // sets the text of the element
$('otherSelector').text($('#routeEnd').val()); // to the value of the input
});
假设要插入文本的元素具有可预测的后缀(例如“txt”)和类,则可以使用以下内容使上述内容更具可扩展性:
<div class="search_container">
<form action="mode" onSubmit="calcRoute();return false;" id="routeForm">
<input type="text" id="routeStart" PlaceHolder="Pikënisja" ><br/>
<input type="text" id="routeEnd" class="routeEnd" PlaceHolder="Destinacioni">
<button id="submit"><i class="icon-map-marker"></i></button>
</form>
</div>
<div class="waypoints" id="routeStartTxt"></div>
<div class="waypoints" id="routeEndTxt"></div>
和 jQuery:
$('#submit').click(function(e){
e.preventDefault(); // prevents form-submission
$('.waypoints').text(function(){ // iterates over each of the .waypoints elements
/* finds the element whose id matches the id of the current .waypoint element,
*without* the 'Txt' suffix, and sets the text of the current .waypoint to
be the value found in that element. */
$('#' + this.id.replace('Txt','')).val();
});
});