Javascript pour définir la valeur de formulaire masqué lors du changement de menu déroulant - options non renseignées à partir d'un tableau Javascript

StackOverflow https://stackoverflow.com/questions/1619372

  •  06-07-2019
  •  | 
  •  

Question

J'ai du code Javascript qui crée 2 tableaux: un pour la catégorie de produit et un pour le produit.

Mais avant que l'utilisateur puisse choisir la catégorie de produit ou le produit, il doit choisir le type de campagne qu'il souhaite utiliser. Ainsi, la sélection (événement) de la «Campagne» déclenche le menu déroulant masqué dans lequel choisir la catégorie de produit. Une fois la catégorie de produit choisie, la liste déroulante Produit se remplit et l'utilisateur est autorisé à choisir parmi les options appropriées.

Mon seul problème: la catégorie de produit ne s'affiche pas correctement. Les options sont invisibles, mais une fois que j'en ai sélectionné une, la liste déroulante Options de produit s'affiche en conséquence. Je suis super confus ....

Voici le code JScript:

<script type="text/javascript">

var info = new Array(
 "Select One*Select One|Select One",
 "Mortgage*1yr NegAm|3yr ARM|5yr ARM|7yr ARM|15yr FRM|30yr FRM",
 "Home Equity*HELoan|HELOC|Convertible",
 "Credit Card*Standard|Premium|Charge|Limited|Secured|Pre-Paid|Business",
 "Student Loan*Subsidized|Unsubsidized|Undergrad|Graduate|Refi",
 "Auto Loan*Purchase|Lease|Used|Dealer"
);

function stringSplit ( string, delimiter ) { 
    if ( string == null || string == "" ) { 
        return null; 
    } else if ( string.split != null ) { 
        return string.split ( delimiter ); 
    } 
} 

var menu1 = new Array();
var menu2 = new Array();

function createMenus() {

    for ( var i=0; i < info.length; i++ ) {
  menu1[i] = stringSplit ( info[i], '*' );
        menu2[i] = stringSplit ( menu1[i][1], '|' );
    }

 var b = document.selector.dropnum.options[document.selector.dropnum.options.selectedIndex].value;

 var myFormx = myForm + b;

 var prodcat = document.myFormx.main;
    var product = document.myFormx.title;

    prodcat.length = menu1.length;
    product.length = menu2[0].length;  

    for ( var i=0; i < menu1.length; i++ ) {
         prodcat.options[i].value  = menu1[i][0];
         prodcat.options[i].text   = menu1[i][0];
    }
    document.myFormx.main.selected = 0;

    for (var x=0; x < menu2[0].length; x++) {
         product.options[x].text = menu2[0][x];
         product.options[x].value = menu2[0][x];
    }
    document.myFormx.title.selected = 0;
}

function updateMenus ( what ) {
    var sel = what.selectedIndex;

    if ( sel >= 0 && sel < menu1.length ) 
        var temp = menu2[sel];
    else
        var temp = new Array ();

    what.form.title.length = temp.length;

    for ( var i = 0; i < temp.length; i++ ) {
        what.form.title.options[i].text  = temp[i];
        what.form.title.options[i].value = temp[i];
    }
    what.form.title.selected=0;
}
</script>

Voici le code de mon fichier .php:

<html>
<body onLoad="createMenus()">

<br>
<br>

<form name="selector" action="#" method="post">       <!-- This is where you add a link to the 'Dials' tab -->
Select the Campaign methodology to model:

<select id='campnum' name='dropnum' class='css_button_example' onChange="javascript: ShowMenu(document.getElementById('campnum').value, 'camp', 5);">
        <option value='0'>Select Campaign
        <option value='1'>Retention Campaign
        <option value='2'>Acquisition Campaign
        <option value='3'>Cross-Sell Campaign
        <option value='4'>Up-Sell Campaign
</select>
</form>

<div name="newboxes" id="camp0" style="display: none;" href="javascript:showonlyone('camp0');">
</div>

<!--*************************************************Retention Campaign************************************************--> 
<div name="newboxes1" id="camp1" style="display: none;" href="javascript:showonlyone('camp1');">
 <form name="myForm1"><p>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 Product Category:&nbsp;
 <select name="main" size=1 onChange="updateMenus(this)">
 <option>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <option>
 <option>
 </select>

 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 Product:&nbsp;
 <select name="title" size=1>
 <option>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <option>
 <option>
 </select>
 </form>
</div>

</body>
</html>

Cordialement,

Vijay

Était-ce utile?

La solution

" href " attribut sur div n'a pas de sens:

<div href="javascript:showonlyone

Et onChange = "javascript: ..." est incorrect - la valeur de onchange doit être uniquement du code JS, pas de "javascript: " préfixe.

Où est défini ShowMenu?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top