2017-04-27 28 views
0

我爲難我的查詢.click()不起作用而難過。我試圖在轉到下一頁之前更改a元素上的href標記。爲什麼jQuery的.click()wouldent目標的標籤?

,這裏是我的jQuery

$('.individualFormSections').click(function() {  
    var formSectionTitle = $(this).siblings('div').text(); // gets section title that was clicked 
    console.log(formSectionTitle); 

    assetConfigIdForURL = assetConfigIdForURL.replace(/\s+/g,'-'); 
    woTypeCodeForURL = woTypeCodeForURL.replace(/\s+/g,'-'); 
    woMaintTypeCode = woMaintTypeCode.replace(/\s+/g,'-'); 
    formSectionTitle = formSectionTitle.replace(/\s+/g,'-'); 

    // Change href dynamically to set url parameters 
    $(this).attr("href",'airSystem.html?insp_asset_config_id='+assetConfigIdForURL+'&wo_type_code='+woTypeCodeForURL+'&wo_maint_type_code='+woMaintTypeCode+'&formSection='+formSectionTitle+'&wo_id='+woIdForURL+''); 

}); 

下面是HTML

  <a class="individualFormSections" href=""> 
       <img class="bus-form-img" src="pull-up.jpg" alt="Trolltunga Norway"> 
      </a> 
      <div class="desc" id="bodyDamageDesc">AirSystem</div> 

我也試着做一個簡單的警告和它甚至不是針對一個標籤。我的JavaScript鏈接設置正確。

有一點背景,html是從以前的javascript函數動態生成的。當我使用Chrome開發人員工具時,所有的html顯示都很好。任何想法可能會導致這種情況?

+0

它的工作,但標籤首先處理的點擊。你需要在你的點擊函數中使用preventDefault() – Cruiser

+1

[* *動態生成*](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements/27373951) –

+0

這是一塊我需要但沒有解決我的問題。可能還有另一個因素,爲什麼它不工作。將回到這個 – plgelso

回答

-1

更改爲此。

 $('.individualFormSections').click(function(e) { 
      e.preventDefault();  
      var formSectionTitle = $(this).siblings('div').text(); // gets section title that was clicked 
      console.log(formSectionTitle); 

      assetConfigIdForURL = assetConfigIdForURL.replace(/\s+/g,'-'); 
      woTypeCodeForURL = woTypeCodeForURL.replace(/\s+/g,'-'); 
      woMaintTypeCode = woMaintTypeCode.replace(/\s+/g,'-'); 
      formSectionTitle = formSectionTitle.replace(/\s+/g,'-'); 

      // Change href dynamically to set url parameters 
      $(this).attr("href",'airSystem.html?insp_asset_config_id='+assetConfigIdForURL+'&wo_type_code='+woTypeCodeForURL+'&wo_maint_type_code='+woMaintTypeCode+'&formSection='+formSectionTitle+'&wo_id='+woIdForURL+''); 

     }); 
+0

改變什麼?儘管這些代碼可能會回答這個問題,但最好解釋一下如何解決問題而不介紹其他人以及爲什麼使用它。從長遠來看,僅有代碼的答案是沒有用的。 –

+0

幫助他人直接從jquery頁面。 描述:如果調用此方法(event.preventDefault()),則不會觸發該事件的默認操作。 此方法不接受任何參數。 例如,點擊的錨點不會將瀏覽器帶到新的URL。我們可以使用event.isDefaultPrevented()來確定此方法是否被該事件觸發的事件處理程序調用。 https://api.jquery.com/event.preventdefault/ – plgelso

0

始終使用防止默認在這種情況下

$('.individualFormSections').click(function(e) { 
    e.preventDefault(); 
    var formSectionTitle = $(this).siblings('div').text(); // gets section title that was clicked 
    console.log(formSectionTitle); 

    assetConfigIdForURL = assetConfigIdForURL.replace(/\s+/g,'-'); 
    woTypeCodeForURL = woTypeCodeForURL.replace(/\s+/g,'-'); 
    woMaintTypeCode = woMaintTypeCode.replace(/\s+/g,'-'); 
    formSectionTitle = formSectionTitle.replace(/\s+/g,'-'); 

    // Change href dynamically to set url parameters 
    $(this).attr("href",'airSystem.html?insp_asset_config_id='+assetConfigIdForURL+'&wo_type_code='+woTypeCodeForURL+'&wo_maint_type_code='+woMaintTypeCode+'&formSection='+formSectionTitle+'&wo_id='+woIdForURL+''); 

});