2013-07-18 28 views
0

我有3個不同的jQuery函數爲我的頁面上的3個不同的textarea設置。它應該工作的方式是你從一些選擇中挑選出來,然後你選擇的選擇應該填充到textarea中以供選擇。問題是它填充你所做的所有3 textareas的選擇。這裏是jsFiddle的鏈接,如果有人能借給我一雙額外的眼睛告訴我我錯過了什麼,我將不勝感激。jQuery 3個不同的功能,但任何3更新所有texareas不是我需要的

jsFiddle Link

HTML

  <div>Sale Locations</div> 
      <TEXTAREA NAME="saleLocation" col="30" ROWS="4" ID="saleLocation"></TEXTAREA> 
      <INPUT NAME="saleLocation_required" TYPE="hidden" VALUE="You cannot leave the field (Sale Location) empty."> 
      <div style="text-align:right; padding-right:10px;"><span class="icon-location-arrow icon-large"></span> <span id="SaleLocationsLink" style="color:red;">choose a location</span> </div> 
      <div id="SaleLocationsDiv" style="background-color:#fff; border:1px dotted black; padding:8px 3px;"> 
       <div style="padding:2px 0px;"> 
       <pre><a href="./" class="SlidedownChoices">Jane Doe 
      P.O. Box 384 
      Acme, BB 666666</a></pre> 
       </div> 
       <div style="padding:2px 0px;"> 
       <pre><a href="./" class="SlidedownChoices">Joe Blow 
      123 Main St 
      Someplace, AA 55555</a></pre> 
       </div> 
      </div> 

      <div>Terms Conditions...</div> 
      <TEXTAREA NAME="termsConditions" ID="termsConditions" ROWS="5"></TEXTAREA> 
      <div style="text-align:right; padding-right:10px;"><span class="icon-location-arrow icon-large"></span> <span id="SalesTermsLink" style="color:red;">choose terms/conditions</span> </div> 
      <div id="SalesTermsDiv" style="background-color:#fff; border:1px dotted black; padding:8px 3px;"> 
       <div style="padding:2px 0px;"> 
       <pre><a href="./" class="SlidedownChoices">Net30</a></pre> 
       </div> 
       <div style="padding:2px 0px;"> 
       <pre><a href="./" class="SlidedownChoices">Cash Only</a></pre> 
       </div> 
       <div style="padding:2px 0px;"> 
       <pre><a href="./" class="SlidedownChoices">Net15</a></pre> 
       </div> 
      </div> 

      <div>Contact...</div> 
      <TEXTAREA NAME="contact" id="contact" ROWS="5"></TEXTAREA> 
      <div style="text-align:right; padding-right:10px;"><span class="icon-location-arrow icon-large"></span> <span id="ContactInformationLink" style="color:red;">choose contact information</span></div> 
      <div id="ContactInformationDiv" style="background-color:#fff; border:1px dotted black; padding:8px 3px;"> 
       <div style="padding:2px 0px;"> 
       <pre><a href="./" class="SlidedownChoices">Mary Jane 
      P.O. Box 69 
      Up High, NY 90210</a></pre> 
       </div> 
      </div> 

JAVASCRIPT

  //code for slide down choices 
      //code for Sale Locations Slide Down 
      if ($("#SaleLocationsDiv").length) { //does the div exist on the page 
       $("#SaleLocationsDiv").hide(); //hide the div if it is not already 
       $("#SaleLocationsLink").click(function() { 
        if ($("#SaleLocationsDiv").is(":hidden")) { //if the div is hidden then slide it down and change text 
         $("#SaleLocationsDiv").slideDown("slow"); 
         $("#SaleLocationsLink").html("hide sale locations"); 
        } else { //if it is not hidden then hide it and change the text back 
         $("#SaleLocationsDiv").slideUp("slow"); 
         $("#SaleLocationsLink").html("choose a location"); 
        } 
       }); 
       //code to add the location to the text area box 
       $("a.SlidedownChoices").click(function (e) { 
        //e is short for event 
        e.preventDefault(); //prevent the click event from going to a url 
        //You want to append the text of the anchor link into the textarea. 
        var innerTxt = $(this).text(); 
        //need to trim whitespace from the string 
        innerTxt = $.trim(innerTxt); 

        var $obj = $("#saleLocation"); //replace this with textarea selector 
        $obj.val($obj.val() + '\n' + innerTxt + '\n'); 
        //reset the sale locations slider 
        $("#SaleLocationsDiv").slideUp("slow"); 
        $("#SaleLocationsLink").html("choose a location"); 
       }); 
      } 

      //code for Terms and Conditions Slide Down 
      if ($("#SalesTermsDiv").length) { //does the div exist on the page 
       $("#SalesTermsDiv").hide(); //hide the div if it is not already 
       $("#SalesTermsLink").click(function() { 
        if ($("#SalesTermsDiv").is(":hidden")) { //if the div is hidden then slide it down and change text 
         $("#SalesTermsDiv").slideDown("slow"); 
         $("#SalesTermsLink").html("hide terms/conditions"); 
        } else { //if it is not hidden then hide it and change the text back 
         $("#SalesTermsDiv").slideUp("slow"); 
         $("#SalesTermsLink").html("choose terms/conditions"); 
        } 
       }); 
       //code to add the terms to the text area box 
       $("a.SlidedownChoices").click(function (e) { 
        //e is short for event 
        e.preventDefault(); //prevent the click event from going to a url 
        //You want to append the text of the anchor link into the textarea. 
        var innerTxt = $(this).text(); 
        //need to trim whitespace from the string 
        innerTxt = $.trim(innerTxt); 

        var $obj = $("#termsConditions"); //replace this with textarea selector 
        $obj.val($obj.val() + '\n' + innerTxt + '\n'); 

        //reset the sale locations slider 
        $("#SalesTermsDiv").slideUp("slow"); 
        $("#SalesTermsLink").html("choose terms/conditions"); 
       }); 
      } 

      //code for Contact Information Slide Down 
      if ($("#ContactInformationDiv").length) { //does the div exist on the page 
       $("#ContactInformationDiv").hide(); //hide the div if it is not already 
       $("#ContactInformationLink").click(function() { 
        if ($("#ContactInformationDiv").is(":hidden")) { //if the div is hidden then slide it down and change text 
         $("#ContactInformationDiv").slideDown("slow"); 
         $("#ContactInformationLink").html("hide contact information"); 
        } else { //if it is not hidden then hide it and change the text back 
         $("#ContactInformationDiv").slideUp("slow"); 
         $("#ContactInformationLink").html("choose contact information"); 
        } 
       }); 
       //code to add the terms to the text area box 
       $("a.SlidedownChoices").click(function (e) { 
        //e is short for event 
        e.preventDefault(); //prevent the click event from going to a url 
        //You want to append the text of the anchor link into the textarea. 
        var innerTxt = $(this).text(); 
        //need to trim whitespace from the string 
        innerTxt = $.trim(innerTxt); 

        var $obj = $("#contact"); //replace this with textarea selector 
        $obj.val($obj.val() + '\n' + innerTxt + '\n'); 

        //reset the sale locations slider 
        $("#ContactInformationDiv").slideUp("slow"); 
        $("#ContactInformationLink").html("choose contact information"); 
       }); 

      } 
      //end code for slide down choices 

回答

0

問題是與你的選擇$("a.SlidedownChoices"),因爲它是註冊3個單擊事件到所有a.SlidedownChoices元素

您甲腎上腺素Fiddle

0

出現這種情況的原因,是因爲所有你選擇的鏈接被稱爲:.SlidedownChoices

因此,如果使用編$("#SaleLocationsDiv a.SlidedownChoices")

嘗試

//code for slide down choices 
//code for Sale Locations Slide Down 
if ($("#SaleLocationsDiv").length) { //does the div exist on the page 
    $("#SaleLocationsDiv").hide(); //hide the div if it is not already 
    $("#SaleLocationsLink").click(function() { 
     if ($("#SaleLocationsDiv").is(":hidden")) { //if the div is hidden then slide it down and change text 
      $("#SaleLocationsDiv").slideDown("slow"); 
      $("#SaleLocationsLink").html("hide sale locations"); 
     } else { //if it is not hidden then hide it and change the text back 
      $("#SaleLocationsDiv").slideUp("slow"); 
      $("#SaleLocationsLink").html("choose a location"); 
     } 
    }); 
    //code to add the location to the text area box 
    $("#SaleLocationsDiv a.SlidedownChoices").click(function (e) { 
     //e is short for event 
     e.preventDefault(); //prevent the click event from going to a url 
     //You want to append the text of the anchor link into the textarea. 
     var innerTxt = $(this).text(); 
     //need to trim whitespace from the string 
     innerTxt = $.trim(innerTxt); 

     var $obj = $("#saleLocation"); //replace this with textarea selector 
     $obj.val($obj.val() + '\n' + innerTxt + '\n'); 
     //reset the sale locations slider 
     $("#SaleLocationsDiv").slideUp("slow"); 
     $("#SaleLocationsLink").html("choose a location"); 
    }); 
} 

//code for Terms and Conditions Slide Down 
if ($("#SalesTermsDiv").length) { //does the div exist on the page 
    $("#SalesTermsDiv").hide(); //hide the div if it is not already 
    $("#SalesTermsLink").click(function() { 
     if ($("#SalesTermsDiv").is(":hidden")) { //if the div is hidden then slide it down and change text 
      $("#SalesTermsDiv").slideDown("slow"); 
      $("#SalesTermsLink").html("hide terms/conditions"); 
     } else { //if it is not hidden then hide it and change the text back 
      $("#SalesTermsDiv").slideUp("slow"); 
      $("#SalesTermsLink").html("choose terms/conditions"); 
     } 
    }); 
    //code to add the terms to the text area box 
    $("#SalesTermsDiv a.SlidedownChoices").click(function (e) { 
     //e is short for event 
     e.preventDefault(); //prevent the click event from going to a url 
     //You want to append the text of the anchor link into the textarea. 
     var innerTxt = $(this).text(); 
     //need to trim whitespace from the string 
     innerTxt = $.trim(innerTxt); 

     var $obj = $("#termsConditions"); //replace this with textarea selector 
     $obj.val($obj.val() + '\n' + innerTxt + '\n'); 

     //reset the sale locations slider 
     $("#SalesTermsDiv").slideUp("slow"); 
     $("#SalesTermsLink").html("choose terms/conditions"); 
    }); 
} 

//code for Contact Information Slide Down 
if ($("#ContactInformationDiv").length) { //does the div exist on the page 
    $("#ContactInformationDiv").hide(); //hide the div if it is not already 
    $("#ContactInformationLink").click(function() { 
     if ($("#ContactInformationDiv").is(":hidden")) { //if the div is hidden then slide it down and change text 
      $("#ContactInformationDiv").slideDown("slow"); 
      $("#ContactInformationLink").html("hide contact information"); 
     } else { //if it is not hidden then hide it and change the text back 
      $("#ContactInformationDiv").slideUp("slow"); 
      $("#ContactInformationLink").html("choose contact information"); 
     } 
    }); 
    //code to add the terms to the text area box 
    $("#ContactInformationDiv a.SlidedownChoices").click(function (e) { 
     //e is short for event 
     e.preventDefault(); //prevent the click event from going to a url 
     //You want to append the text of the anchor link into the textarea. 
     var innerTxt = $(this).text(); 
     //need to trim whitespace from the string 
     innerTxt = $.trim(innerTxt); 

     var $obj = $("#contact"); //replace this with textarea selector 
     $obj.val($obj.val() + '\n' + innerTxt + '\n'); 

     //reset the sale locations slider 
     $("#ContactInformationDiv").slideUp("slow"); 
     $("#ContactInformationLink").html("choose contact information"); 
    }); 

} 
//end code for slide down choices 

演示爲目標的特定元素一個被點擊,它觸發所有三個功能,並因此將結果附加到每個文本區域。只需將每個部分的.SlidedownChoices重命名爲一個獨特的類,它就可以工作。

或者,您可能將所有功能放入一個,因爲它們基本上完全相同。您可以使用數據元素來觸發特定的文本區域和選擇。

希望這會有所幫助。

0

您的點擊功能具有與三個框中的所有鏈接相匹配的選擇器。因此,當用戶點擊任何選項時,所有3個功能都會運行並更改相應文本區域中的值。

您需要爲每個選擇不同的選擇器

例如,對於銷售地點使用:

$("#SaleLocationsDiv a.SlidedownChoices").click(function (e) 

instad

$("a.SlidedownChoices").click(function (e) 

的jsfiddle環線:JsFiddle

相關問題