Bonjour,
Dans un formulaire d'insertion d'annonces, l'utilisateur a la possibilité d'indiquer plusieurs périodes qui sont liée à un type de saison :
A chaque nouvelle saison choisie, j'ajoute automatiquement des champs pour l'encodage du prix et de la durée qui y correspondent sur le onblur du choix du type de saison :
Tout fonctionne bien et les champs suivant sont correctement créés :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 $(document).ready(function() { $("body").on( 'blur', '.periodSeason',function() { var periods = $('.periodSeason').map(function(){ if($(this).val()) { return $(this).val() } }).get(); //alert($.inArray($(this).val(), periods)); //alert($.inArray($(this).val(), periods)); var nbOcc = 0; for (var i = 0; i < periods.length; i++) { if (periods[i] == $(this).val()) { nbOcc++; } } if (nbOcc < 2) { // Get the ul that holds the collection of prices collectionHolderPrices = $ ( 'ul.prices' ); var $newLinkLiPrice = $ ( '<li></li>' ) // add the "add a period" anchor and li to the periods ul collectionHolderPrices . append ( $newLinkLiPrice ); // count the current form inputs we have (e.g. 2), use that as the new // index when inserting a new item (e.g. 2) collectionHolderPrices . data ( 'index' , collectionHolderPrices . find ( ':input' ). length ); // Get the data-prototype explained earlier var prototype = collectionHolderPrices . data ( 'prototype' ); // get the new index var index = collectionHolderPrices . data ( 'index' ); var newForm = prototype ; // You need this only if you didn't set 'label' => false in your photos field in AdvertType // Replace '__name__label__' in the prototype's HTML to // instead be a number based on how many items we have // newForm = newForm.replace(/__name__label__/g, index); // Replace '__name__' in the prototype's HTML to // instead be a number based on how many items we have newForm = newForm . replace ( /__name__/g , index ); // increase the index with one for the next item collectionHolderPrices . data ( 'index' , index + 1 ); // Display the form in the page in an li, before the "Add a Period" link li var $newFormLi = $ ( '<li></li>' ). append ( newForm ); $newLinkLiPrice . before ( $newFormLi ); } }); });
Ma question est de savoir comment donner une valeur par défaut à un des champs créés par ce script? Je désire que la valeur de la saison choisie se retrouve comme valeur par défaut dans le champ "season" du prix pour savoir à quelle saison le prix se rapporte.
Quelqu'un aurait une idée?
Merci d'avance pour votre aide.
Partager