我完全是jQuery的新手,所以請裸照我。我試圖有條件地顯示/隱藏文本字段,但最初隱藏它們。如果頁面被刷新,我已經達到了它可以工作的程度,但它最初會顯示所有應該隱藏的字段:http://joelmorehouse.com/soderholminsurance/get-a-quote。jQuery隱藏不工作,直到刷新
我在/ wordpress/wp-content/themes/my-theme/js /中爲我的主題添加了jQuery 1.7.1。
我創建了一個名爲hidefieldsScript.js的腳本,並將其放入我的主題的js文件夾中:http://joelmorehouse.com/soderholminsurance/wp-content/themes/bridge/js/hidefieldsScript.js。
,我將這些代碼在身體在我的header.php頂部:
<!-- Add jquery script to support Conditional Forms--> <script type="text/javascript" src="<?php echo get_stylesheet_directory_uri(); ?>/js/1.7.1/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="<?php echo get_stylesheet_directory_uri(); ?>/js/hidefieldsScript.js"></script>
這裏是我使用的聯繫表格7
<div id="contactForm">
<h3>Get a Free Quote</h3>
<ul>
<li>
<label for="your-name">Your Name<span style="color:#ff0000;">*</span></label>[text* your-name id:your-name class:contactForm ""]
</li>
<li>
<label for="insurance-type">Type of Insurance<span style="color:#ff0000;">*</span></label>[select* insurance-type id:insurance-type include_blank class:contactForm "Auto Insurance" "Home Insurance" "Life Insurance" "Commercial Insurance" "Health Insurance" "Bond" "Other"]
</li>
<li>
<div class="hide" id="hide-auto1">
<label for="number-of-drivers">Number of Drivers<span style="color:#ff0000;">*</span></label>[text number-of-drivers id:number-of-drivers class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-auto2">
<label for="number-of-vehicles">Number of Vehicles<span style="color:#ff0000;">*</span></label>[text number-of-vehicles id:number-of-vehicles class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-auto3">
<label for="currently-insured-auto">Currently Insured? (Yes/No)<span style="color:#ff0000;">*</span></label>[text currently-insured-auto id:currently-insured-auto class:contactForm ""]
</div>
</li>
<!-- HOME INSURANCE FIELDS -->
<li>
<div class="hide" id="hide-home1">
<label for="age-of-home">Age of Home<span style="color:#ff0000;">*</span></label>[text age-of-home /50 id:age-of-home class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-home2">
<label for="dwelling-value">Dwelling Value<span style="color:#ff0000;">*</span></label>[text dwelling-value /50 id:dwelling-value class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-home3">
<label for="owner-occupied">Owner Occupied? (Yes/No)<span style="color:#ff0000;">*</span></label>[text owner-occupied /50 id:owner-occupied class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-home4">
<label for="new-purchase">New Purchase (If yes, please provide closing date.)<span style="color:#ff0000;">*</span></label>[text new-purchase /50 id:new-purchase class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-home5">
<label for="currently-insured-home">Currently Insured? (Yes/No)<span style="color:#ff0000;">*</span></label>[text currently-insured-home /50 id:currently-insured-home class:contactForm ""]
</div>
</li>
<!-- LIFE INSURANCE FIELDS -->
<li>
<div class="hide" id="hide-life1">
<label for="dob-life">Date of Birth<span style="color:#ff0000;">*</span></label>[text dob-life /50 id:dob-life class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-life2">
<label for="smoker-life">Smoker? (Yes/No)<span style="color:#ff0000;">*</span></label>[text smoker-life /50 id:smoker-life class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-life3">
<label for="amount-desired">Amount of Insurance Desired<span style="color:#ff0000;">*</span></label>[text amount-desired /50 id:amount-desired class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-life4">
<label for="currently-insured-life">Current Life Insurance Policy? (Yes/No)<span style="color:#ff0000;">*</span></label>[text currently-insured-life /50 id:currently-insured-life class:contactForm ""]
</div>
</li>
<!-- COMMERCIAL INSURANCE FIELDS -->
<li>
<div class="hide" id="hide-com1">
<label for="business-type">Type of Business<span style="color:#ff0000;">*</span></label>[text business-type /50 id:business-type class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-com2">
<label for="years-in-business">Years in Business<span style="color:#ff0000;">*</span></label>[text years-in-business /50 id:years-in-business class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-com3">
<label for="federal-id">Federal ID#<span style="color:#ff0000;">*</span></label>[text federal-id /50 id:federal-id class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-com4">
<label for="business-description">Business Operations Description<span style="color:#ff0000;">*</span></label>[text business-description /50 id:business-description class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-com5">
<label for="number-of-employees">Number of Employees<span style="color:#ff0000;">*</span></label>[text number-of-employees /50 id:number-of-employees class:contactForm ""]
</div>
</li>
<!-- HEALTH INSURANCE FIELDS -->
<li>
<div class="hide" id="hide-health1">
<label for="medicare">Medicare? (Yes/No)<span style="color:#ff0000;">*</span></label>[text medicare /50 id:medicare class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-health2">
<label for="dob-health">Date of Birth<span style="color:#ff0000;">*</span></label>[text dob-health /50 id:dob-health class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-health3">
<label for="smoker-health">Smoker? (Yes/No)<span style="color:#ff0000;">*</span></label>[text smoker-health /50 id:smoker-health class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-health4">
<label for="deductible">Deductible<span style="color:#ff0000;">*</span></label>[text deductible /50 id:deductible class:contactForm ""]
</div>
</li>
<!-- BOND -->
<li>
<div class="hide" id="hide-bond1">
<label for="address">Address<span style="color:#ff0000;">*</span></label>[text address /50 id:address class:contactForm ""]
</div>
</li>
<!-- OTHER -->
<li>
<div class="hide" id="hide-other1">
<label for="other-types">Type(s) of Insurance<span style="color:#ff0000;">*</span></label>[text other-types /50 id:other-types class:contactForm ""]
</div>
</li>
<li>
<div class="hide" id="hide-other2">
<label for="description-of-coverage">Description of Coverage<span style="color:#ff0000;">*</span></label>[text description-of-coverage /50 id:description-of-coverage class:contactForm ""]
</div>
</li>
<!-- EMAIL ADDRESS -->
<li>
<label for="your-email">Your Email Address<span style="color:#ff0000;">*</span></label>[email* your-email /50 id:your-email class:contactForm ""]
</li>
<li>
<!-- PHONE -->
<li>
<label for="your-phone">Your Phone Number<span style="color:#ff0000;">*</span></label>[text* your-phone id:your-phone class:contactForm ""]
</li>
<li>
<!-- ADDITIONAL NOTES -->
<label for="additional-notes" style="padding-top: .5em;">Additional Notes</label>[textarea* additional-notes id:additional-notes class:contactForm "Optional"]
</li>
<li>
</ul>
<span style="color:#ff0000;">* = Required Field</span>
</div>
[submit]
這是怎麼回事?我讀了一些地方把腳本放在header.php的頭部,而不是頭部,所以我沒有成功。我也發現了一個類似的帖子,但他們的問題是無效的ID。任何幫助將非常感激!
我試着去你的網站,我看到幾個控制檯錯誤 - 但頁面只是不斷刷新,所以我不能讀什麼他們及時 - 首先處理控制檯錯誤。另外,使用WordPress你可能會「排隊」你的腳本,而且很可能已經包含了jQuery。 – patricksweeney 2014-12-07 01:31:14
感謝您的快速回復。應該解決頁面刷新的錯誤(我嘗試添加一個插件,刷新頁面一秒後,但它只是繼續循環)。我不確定您的控制檯錯誤是什麼意思,並排列我的腳本。對不起,這對我來說都是全新的。 – joelmorehouse 2014-12-07 01:35:54
我對WordPress並不熟悉,但你說你已經把Javascript放在'js'文件夾中。那麼爲什麼你使用'css'文件夾,'get_stylesheet_directory_uri()'? – 2014-12-07 01:53:00