2013-12-05 3 views
0

Я уверен, что это, наверное, что-то простое, я не замечаю, но я не могу понять это для жизни меня. Любая помощь приветствуется.jquery добавление переменных в атрибут href

HTML

<h1 class="entry-title">Installation Maintenance and Detailed Inspection</h1> 
<div class="ai1ec-time"> 
    <div class="ai1ec-label">When:</div> 
    <div class="ai1ec-field-value">January 13, 2014</div> 
</div> 
<div class="ai1ec-location"> 
    <div class="ai1ec-label">Where:</div> 
    <div class="ai1ec-field-value">MOXI Perth</div> 
</div> 
<div class="ai1ec-cost"> 
    <div class="ai1ec-label">Cost:</div> 
    <div class="ai1ec-field-value">AU$3200</div>  
</div> 
<a id="booklink" title="Training Enrolement Form" href="http://www.moxi.com.au/training-enrolement-form/">Book Now!</a> 

JQuery

<script> 
jQuery(document).ready(function(){ 
    var bookcourse = jQuery(".entry-title").html; 
    var booktime = jQuery(".ai1ec-time .ai1ec-field-value").html; 
    var booklocation = jQuery(".ai1ec-location .ai1ec-field-value").html; 
    var bookcost = jQuery(".ai1ec-cost .ai1ec-field-value").html; 
    jQuery("#booklink").attr('href',"http://www.moxi.com.au/training-enrolement-form?title="+bookcourse+"&cost="+bookcost+"&date="+booktime+"&location="+booklocation); 
}); 
</script> 

Цель заключается иметь HREF атрибута выйти как:

http://www.moxi.com.au/training-enrolement-form?title=Installation Maintenance and Detailed Inspection&cost=AU$3200&date=January 13, 2014&location=MOXI Perth 
+1

Почему бы не использовать 'text()'? вы даже включите разметку html в ссылку, если будете использовать 'html()' –

ответ

0

html() это метод не является свойством, так что вам нужно использование .html() не .html

var bookcourse = jQuery(".entry-title").html(); 
var booktime = jQuery(".ai1ec-time .ai1ec-field-value").html(); 
var booklocation = jQuery(".ai1ec-location .ai1ec-field-value").html(); 
var bookcost = jQuery(".ai1ec-cost .ai1ec-field-value").html(); 

Демо: Fiddle

+0

Да ... что-то такое глупое. Определенно показывает, что я слишком долго смотрел на это. –

0

попробовать

jQuery(document).ready(function(){ 
var bookcourse = $(".entry-title").html(); 
var booktime = $(".ai1ec-time .ai1ec-field-value").html(); 
var booklocation = $(".ai1ec-location .ai1ec-field-value").html(); 
var bookcost = $(".ai1ec-cost .ai1ec-field-value").html(); 
jQuery("#booklink").attr('href', "http://www.moxi.com.au/training-enrolement-form?title=" + bookcourse + "&cost=" + bookcost + "&date=" + booktime + "&location=" + booklocation); 

}); 
0

Try:

var href = $("#booklink").attr("href"); 
if (href.substr(0, href.length - 1) == "/") { 
    href = href.substr(0, href.length - 1); 
} 
href += "?title=" + $(".entry-title").text(); 
href += "?cost=" + $(".ai1ec-cost .ai1ec-field-value").text(); 
href += "?date=" + $(".ai1ec-time .ai1ec-field-value").text(); 
href += "?location=" + $(".ai1ec-location .ai1ec-field-value").text(); 
$("#booklink").attr("href", href); 

DEMO here.

0

Попробуйте

вы можете использовать .text() метод также

$(document).ready(function() 
    var bookcourse = $(".entry-title").html(); 
    var booktime = $(".ai1ec-time .ai1ec-field-value").html(); 
    var booklocation = $(".ai1ec-location .ai1ec-field-value").html(); 
    var bookcost = $(".ai1ec-cost .ai1ec-field-value").html(); 
    $("#booklink").attr('href', "http://www.moxi.com.au/training-enrolement-form?title=" + bookcourse + "&cost=" + bookcost + "&date=" + booktime + "&location=" + booklocation); 
}); 
0

.html() является метод, и вы должны вызывать его как метод, так что вы должны написать

jQuery(".entry-title").html();

и в стороне от него .html()возвращает все html-разметка внутри элемента, я думаю, вы должны использовать text(), вместо этого текст будет включен только в ваш url

Смежные вопросы