任何理由为什么jquery .click()wouldent针对一个标签?

时间:2017-04-27 13:00:59

标签: javascript jquery html twitter-bootstrap

我很难过为什么我的查询.click()不起作用。我想在一个元素上更改href标记,然后再转到下一页。

这是我的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显示都很好。关于可能导致这种情况的任何想法?

2 个答案:

答案 0 :(得分: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+'');

 });

答案 1 :(得分:-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+'');

         });