2016-07-01 15 views
1

Ich bin ein Anfänger! und ich stecke seit vielen Tagen auf dieser Frage fest. und ich brauche dringend Hilfe! Diese Frage könnte ein wenig schwierig zu verstehen sein. Bitte lesen Sie sorgfältig.DROPDOWN-Liste in HTML mit anderen HTML-Seiten verknüpfen

Ich habe eine Dropdown-Liste DDL1 (eine Dropdown-Liste von 5 Staaten) und eine DYNAMIC DROPDOWNLIST DDL2 (die aus Orten in jedem Zustand besteht) erstellt. DDL1 ist mit DDL2 so verknüpft, dass sich die Werte von ddl2 beim Auswählen des Status in ddl1 ändern. FÜR zB: wenn u GOA in DDL1 wählen u sehen Orte von goa in DDL2 (SIE DIE CODES UNTER DER VERKNÜPFUNG SEHEN)

Ich möchte die Möglichkeiten der DDL2 dh (die Orte), wenn sie ausgewählt und Absenden-Button wird gedrückt, um einige damit verbundene HTML-Seiten zu öffnen.

darunter habe ich auch noch eine Dropdown-Liste, die ive erfolgreich verknüpft, weil es keine dynamisch verknüpfte Dropdown-Liste ist. Und Verknüpfung, die war einfach, weil es in Select-Tag ist. während der ddl2 im Skript-Tag ist, da er dynamisch mit ddl1 verbunden ist.

kann mir jemand bitte den Code für die Verknüpfung der ddl2 (d. H. Goa asf, goa lpg ro etc) zu anderen HTML-Seiten sagen?

<!DOCTYPE HTML> 
    <HTML> 
    <HEAD> 
    <TITLE> STATES</TITLE> 
    <script type="text/javascript"> 
    function configureDropDownLists(ddl1,ddl2) { 
    var goa = ['GOA ASF', 'Goa LPG Plant']; 
    var maharashtra = ['AKOLA IRD', 'AURANGABAD LPG PLANT''WADALA I TERMINAL']; 
var rajasthan = ['AJMER LPG PLANT ','AJMER TERMINAL', 'AWA-SALAWAS PIPELINE PROJ']; 
var gujrat = ['AHMEDABAD NWZ LPG ', 'AHMEDABAD NWZ RETAIL', 'AHMEDABAD RETAIL RO']; 
var madhyapradesh =['BAKANIA RIL', 'BHOPAL DSRO', 'BHOPAL RRO']; 

    switch (ddl1.value) { 
    case 'Goa': 
     ddl2.options.length = 0; 
     for (i = 0; i < goa.length; i++) { 
      createOption(ddl2, goa[i],goa[i]); 
     } 
     break; 
    case 'Maharashtra': 
     ddl2.options.length = 0; 
    for (i = 0; i < maharashtra.length; i++) { 
     createOption(ddl2, maharashtra[i],maharashtra[i]); 
     } 
     break; 
    case 'Rajasthan': 
     ddl2.options.length = 0; 
     for (i = 0; i < rajasthan.length; i++) { 
      createOption(ddl2, rajasthan[i],rajasthan[i]); 
     } 
     break; 
    case 'Gujrat': 
     ddl2.options.length = 0; 
     for (i = 0; i < gujrat.length; i++) { 
      createOption(ddl2, gujrat[i],gujrat[i]); 
     } 
     break; 
    case 'MadhyaPradesh': 
     ddl2.options.length = 0; 
     for (i = 0; i < madhyapradesh.length; i++) { 
      createOption(ddl2, madhyapradesh[i],madhyapradesh[i]) 
      } 
     break; 
     default: 
      ddl2.options.length = 0; 
     break; 
     } 

     } 

    function createOption(ddl, text, value) { 
    var opt = document.createElement('option'); 
    opt.value = value; 
    opt.text = text; 
    ddl.options.add(opt); 
    } 
    </script> 
    </HEAD> 

    <BODY> 
    <div> 
    <H1><FONT="TIMES ROMAN" FONT-COLOR="BLUE" > SELECT A STATE:</H1> 

     <select id="ddl" onchange="configureDropDownLists(this,document.getElementById('ddl2'))"> 
     <option value=""></option> 
     <option value="Goa">Goa</option> 
     <option value="Maharashtra">Maharashtra</option> 
     <option value="Rajasthan">Rajasthan</option> 
     <option value="Gujrat">Gujrat</option> 
     <option value="MadhyaPradesh">MadhyaPradesh</option> 
     </select> 

     <select id="ddl2"> 
     </select><br> 
     <br> 
     <input class="SubmitButton" type="submit" name="SUBMITBUTTON" value="Submit" style="font-size:20px; " /> 
     </div> 
     <div> 
     <H1><FONT="TIMES ROMAN" FONT-COLOR="BLUE" > SELECT An ASSET:</H1> 
    <form id="link"> 
    <select multiple="multiple" size="1"> 

    <option value="http://stackoverflow.com/">4GB RAM PC- Lot 500 HCL</option> 
    <option value="http://google.com/">4GB RAM PC- Lot 450 HCL</option> 
    <option value="http://yahoo.com/">HD 245 Gold Lot 50</option> 
    <option value="http://bing.com/">Marathon 255 (40)</option> 
    <option value="http://php.net/">Wep HQ 2100 (20)</option> 
    <option value="ADF Scanner (45)">ADF Scanner (45)</option>  
    </select><BR> 

    <br> 
    <input class="SubmitButton" type="submit" name="SUBMITBUTTON" value="Submit" style="font-size:20px; "> 
    </form> 
    </div> 
    <script src="http://code.jquery.com/jquery-3.0.0.min.js"></script> 
    <script> 
$('#link').on('submit', function (e) { 
    e.preventDefault(); 
    var $form = $(this), 
      $select = $form.find('select'), 
      links = $select.val(); 
    if (links.length > 0) { 
     for (i in links) { 
      link = links[i]; 
      window.open(link); 
     } 
    } 
    }); 
    </script> 

    </BODY> 
    </HTML> 

Der obige Code meine ganze ist Code-Seiten gegeben:

die Codes meiner Seite weiter unten. Wenn Sie es in HTML ausführen, werden Sie genau wissen, was ich erstellt habe und was ich verlinken möchte.
Bitte starten Sie es und helfen Sie mir mit den Codes und seiner Struktur, wenn Sie können. Danke

+0

manchmal ist es sehr gut enthalten Code auf jsfiddle.net zu veröffentlichen und bieten ein Link hier –

Antwort

1

IMHO mischen Sie nicht Javascript und jQuery. Sie haben bereits die jQuery-Bibliothek importiert, damit Sie sie optimal für kaskadierende Effekte nutzen können.

jQuery-Code:

jQuery(function($) { 

    // bind change event to select 
    $('#location').on('change', function() { 
    var url = $(this).val(); // get selected value 
    if (url) { // require a URL 
     window.location = url; // redirect 
    } 
    return false; 
    }); 

    var hashtable = {}; 
    hashtable['GOA ASF'] = 'https://stackoverflow.com/'; 
    hashtable['Goa LPG Plant'] = 'http://google.com/'; 
    hashtable['AKOLA IRD'] = 'http://yahoo.com/'; 
    hashtable['AURANGABAD LPG PLANT'] = 'http://yahoo.com/'; 
    hashtable['WADALA I TERMINAL'] = 'http://yahoo.com/'; 
    hashtable['AJMER LPG PLANT'] = 'http://yahoo.com/'; 
    hashtable['AJMER TERMINAL'] = 'http://yahoo.com/'; 
    hashtable['AWA-SALAWAS PIPELINE PROJ'] = 'http://yahoo.com/'; 
    hashtable['AHMEDABAD NWZ LPG'] = 'http://yahoo.com/'; 
    hashtable['AHMEDABAD NWZ RETAIL'] = 'http://yahoo.com/'; 
    hashtable['AHMEDABAD RETAIL RO'] = 'http://yahoo.com/'; 
    hashtable['BAKANIA RIL'] = 'http://google.com/'; 
    hashtable['BHOPAL DSRO'] = 'http://google.com/'; 
    hashtable['BHOPAL RRO'] = 'http://google.com/'; 

    var locations = { 
    'Goa': ['GOA ASF', 'Goa LPG Plant'], 
    'Maharashtra': ['AKOLA IRD', 'AURANGABAD LPG PLANT', 'WADALA I TERMINAL'], 
    'Rajasthan': ['AJMER LPG PLANT', 'AJMER TERMINAL', 'AWA-SALAWAS PIPELINE PROJ'], 
    'Gujrat': ['AHMEDABAD NWZ LPG', 'AHMEDABAD NWZ RETAIL', 'AHMEDABAD RETAIL RO'], 
    'MadhyaPradesh': ['BAKANIA RIL', 'BHOPAL DSRO', 'BHOPAL RRO'] 

    } 

    var $locations = $('#location'); 
    $('#country').change(function() { 
    var country = $(this).val(), 
     lcns = locations[country] || []; 

    var html = $.map(lcns, function(lcn) { 
     return '<option value="' + hashtable[lcn] + '">' + lcn + '</option>' 
    }).join(''); 
    $locations.html(html) 
    }); 
}); 

HTML-Code:

<label class="page1">SELECT A STATE:</label> 
<div class="tooltips" title="Please select the country that the customer will primarily be served from"> 
    <select id="country" name="country" placeholder="Phantasyland"> 
    <option></option> 
    <option>Goa</option> 
    <option>Maharashtra</option> 
    <option>Rajasthan</option> 
    <option>Gujrat</option> 
    <option>MadhyaPradesh</option> 
    </select> 
</div> 
<br /> 
<br /> 
<label class="page1">Location</label> 
<div class="tooltips" title="Please select the city that the customer is primarily to be served from."> 
    <select id="location" name="location" placeholder="Anycity"></select> 
</div> 

komplette HTML-Datei:

<HTML> 
    <HEAD> 
    <TITLE>STATES</TITLE> 
    <script src="http://code.jquery.com/jquery-3.0.0.min.js"></script> 
    <script> 
     jQuery(function($) { 

     // bind change event to select 
     $('#location').on('change', function() { 
     var url = $(this).val(); // get selected value 
     if (url) { // require a URL 
     window.location = url; // redirect 
     } 
     return false; 
     }); 

     var hashtable = {}; 
     hashtable['GOA ASF'] = 'https://stackoverflow.com/'; 
     hashtable['Goa LPG Plant'] = 'http://google.com/'; 
     hashtable['AKOLA IRD'] = 'http://yahoo.com/'; 
     hashtable['AURANGABAD LPG PLANT'] = 'http://yahoo.com/'; 
     hashtable['WADALA I TERMINAL'] = 'http://yahoo.com/'; 
     hashtable['AJMER LPG PLANT'] = 'http://yahoo.com/'; 
     hashtable['AJMER TERMINAL'] = 'http://yahoo.com/'; 
     hashtable['AWA-SALAWAS PIPELINE PROJ'] = 'http://yahoo.com/'; 
     hashtable['AHMEDABAD NWZ LPG'] = 'http://yahoo.com/'; 
     hashtable['AHMEDABAD NWZ RETAIL'] = 'http://yahoo.com/'; 
     hashtable['AHMEDABAD RETAIL RO'] = 'http://yahoo.com/'; 
     hashtable['BAKANIA RIL'] = 'http://google.com/'; 
     hashtable['BHOPAL DSRO'] = 'http://google.com/'; 
     hashtable['BHOPAL RRO'] = 'http://google.com/'; 

     var locations = { 
     'Goa': ['GOA ASF', 'Goa LPG Plant'], 
     'Maharashtra': ['AKOLA IRD', 'AURANGABAD LPG PLANT', 'WADALA I TERMINAL'], 
     'Rajasthan': ['AJMER LPG PLANT', 'AJMER TERMINAL', 'AWA-SALAWAS PIPELINE PROJ'], 
     'Gujrat': ['AHMEDABAD NWZ LPG', 'AHMEDABAD NWZ RETAIL', 'AHMEDABAD RETAIL RO'], 
     'MadhyaPradesh': ['BAKANIA RIL', 'BHOPAL DSRO', 'BHOPAL RRO'] 

     } 

     var $locations = $('#location'); 
     $('#country').change(function() { 
     var country = $(this).val(), 
     lcns = locations[country] || []; 

     var html = $.map(lcns, function(lcn) { 
     return '<option value="' + hashtable[lcn] + '">' + lcn + '</option>' 
     }).join(''); 
     $locations.html(html) 
     }); 
     }); 

    </script> 
    </HEAD> 
    <BODY> 
    <label class="page1">SELECT A STATE:</label> 
    <div class="tooltips" title=""> 
     <select id="country" name="country" placeholder="Phantasyland"> 
     <option></option> 
     <option>Goa</option> 
     <option>Maharashtra</option> 
     <option>Rajasthan</option> 
     <option>Gujrat</option> 
     <option>MadhyaPradesh</option> 
     </select> 
    </div> 
    <br /> 
    <br /> 
    <label class="page1">Location</label> 
    <div class="tooltips" title="Please select the city that the customer is primarily to be served from."> 
     <select id="location" name="location" placeholder="Anycity"></select> 
    </div> 
    </BODY> 
</HTML> 

Bitte beachten Sie die folgenden Lösungen jQuery verwenden.

How to populate a cascading Dropdown with JQuery

Demo fiddle hier

+0

http://stackoverflow.com/questions/18351921/how-to-populate-a-cascading-dropdown-with-jquery zeigt, wie Sie die beiden Dropdown-Liste verbinden. was ich schon getan habe. Ich möchte nur die 2. verbundenen Dropdown-Liste, um HTML-Seiten zu öffnen. Werden deine Codes das tun? –

+0

bitte überprüfen ive bereits verknüpft die Standorte und die Staaten Dropdown-Liste. Und es funktioniert bereits gut. Mein Problem ist, dass die ddl2 HTML-Seiten öffnen sollte, wenn auf die Schaltfläche zum Senden geklickt wird. –

+0

@NehaShetty - bitte überprüfen Sie den Code jetzt. Lass mich wissen ob es funktioniert. –

0

Sie haben ein paar Probleme im Code:

  1. Syntaxfehler auf 'Maharashtra' Array;
  2. Sie müssen Ihre dynamische Auswahl (ddl2) mit einem Formular umschließen.
  3. Bearbeiten Sie den Wert von ddl2, um eine Verknüpfung statt Zeichenfolge zu enthalten.
  4. Wie die Antwort oben, gemischte jquery und js.

ich bearbeitet Ihr Code die Form und hat eine Verbindung zum Beispiel in Zeile 12 (‚Goa‘ Option)

<HTML> 
<HEAD> 
    <TITLE> STATES</TITLE> 
    <script type="text/javascript"> 
     function configureDropDownLists(ddl1, ddl2) { 
      var goa = ['GOA ASF', 'Goa LPG Plant']; 
      var maharashtra = ['AKOLA IRD', 'AURANGABAD LPG PLANT', 'WADALA I TERMINAL']; 
      var rajasthan = ['AJMER LPG PLANT ', 'AJMER TERMINAL', 'AWA-SALAWAS PIPELINE PROJ']; 
      var gujrat = ['AHMEDABAD NWZ LPG ', 'AHMEDABAD NWZ RETAIL', 'AHMEDABAD RETAIL RO']; 
      var madhyapradesh = ['BAKANIA RIL', 'BHOPAL DSRO', 'BHOPAL RRO']; 
      var linkExample = 'http://google.com/'; 

      switch (ddl1.value) { 
       case 'Goa': 
        ddl2.options.length = 0; 
        for (i = 0; i < goa.length; i++) { 
         createOption(ddl2, goa[i], linkExample); 
        } 
        break; 
       case 'Maharashtra': 
        ddl2.options.length = 0; 
        for (i = 0; i < maharashtra.length; i++) { 
         createOption(ddl2, maharashtra[i], maharashtra[i]); 
        } 
        break; 
       case 'Rajasthan': 
        ddl2.options.length = 0; 
        for (i = 0; i < rajasthan.length; i++) { 
         createOption(ddl2, rajasthan[i], rajasthan[i]); 
        } 
        break; 
       case 'Gujrat': 
        ddl2.options.length = 0; 
        for (i = 0; i < gujrat.length; i++) { 
         createOption(ddl2, gujrat[i], gujrat[i]); 
        } 
        break; 
       case 'MadhyaPradesh': 
        ddl2.options.length = 0; 
        for (i = 0; i < madhyapradesh.length; i++) { 
         createOption(ddl2, madhyapradesh[i], madhyapradesh[i]) 
        } 
        break; 
       default: 
        ddl2.options.length = 0; 
        break; 
      } 

      ddl2.setAttribute('multiple', 'multiple'); 
      ddl2.setAttribute('size', 1); 
     } 

     function createOption(ddl, text, value) { 
      var opt = document.createElement('option'); 
      opt.value = value; 
      opt.text = text; 
      ddl.options.add(opt); 
     } 
    </script> 
</HEAD> 

<BODY> 
<div> 
    <H1><FONT="TIMES ROMAN" FONT-COLOR="BLUE" > SELECT A STATE:</H1> 

    <select id="ddl" onchange="configureDropDownLists(this,document.getElementById('ddl2'))"> 
     <option value=""></option> 
     <option value="Goa">Goa</option> 
     <option value="Maharashtra">Maharashtra</option> 
     <option value="Rajasthan">Rajasthan</option> 
     <option value="Gujrat">Gujrat</option> 
     <option value="MadhyaPradesh">MadhyaPradesh</option> 
    </select> 

    <form id="link1"> 
     <select id="ddl2" multiple='multiple'> 
     </select><br> 
     <br> 
     <input class="SubmitButton" type="submit" name="SUBMITBUTTON" value="Submit" style="font-size:20px; "/> 
    </form> 
</div> 
<div> 
    <H1><FONT="TIMES ROMAN" FONT-COLOR="BLUE" > SELECT An ASSET:</H1> 

    <form id="link"> 
     <select multiple="multiple" size="1"> 

      <option value="http://stackoverflow.com/">4GB RAM PC- Lot 500 HCL</option> 
      <option value="http://google.com/">4GB RAM PC- Lot 450 HCL</option> 
      <option value="http://yahoo.com/">HD 245 Gold Lot 50</option> 
      <option value="http://bing.com/">Marathon 255 (40)</option> 
      <option value="http://php.net/">Wep HQ 2100 (20)</option> 
      <option value="ADF Scanner (45)">ADF Scanner (45)</option> 
     </select><BR> 

     <br> 
     <input class="SubmitButton" type="submit" name="SUBMITBUTTON" value="Submit" style="font-size:20px; "> 
    </form> 
</div> 
<script src="http://code.jquery.com/jquery-3.0.0.min.js"></script> 
<script> 
    $('#link1').on('submit', function (e) { 
     var link = $('#ddl2').val(); 
     window.open(link); 
    }); 


    $('#link').on('submit', function (e) { 
     e.preventDefault(); 
     var $form = $(this), 
       $select = $form.find('select'), 
       links = $select.val(); 
     if (links.length > 0) { 
      for (i in links) { 
       link = links[i]; 
       window.open(link); 
      } 
     } 
    }); 
</script> 

</BODY> 
</HTML> 
+0

Hallo mein Herr! Ich habe Zweifel, wann wird das eröffnet werden? var linkExample = 'http://google.com/'; . Ich sehe nicht goa asf oder andere Standorte verlinkt zu jeder Seite. Wie wird das Klicken auf sie irgendeine Seite öffnen, Herr? –

+0

Ich habe den Link Beispiel in diesem Teil: ** Fall 'Goa': ddl2.options.length = 0; für (i = 0; i

+0

https://jsfiddle.net/nehashetty/8ft8zgqb/ Herr bitte überprüfen Sie den Code in Geige und auch it.it verschiebt die ddl2 Drop-down und macht die nicht zeigen die Optionen in es.also bitte sagen Sie mir die Korrekturen –

Verwandte Themen