0

I have a simple filesector i have made (with links as the file names). When i click a filename the following javascript is run:

<script>
    $(function () {
        $(".fileSelect").click(function () {
            var name = $(this).text(); //gets the name of the clicked file
            var id = $(this).attr("id");// gets the id of the clicked file
            $("#filehiddenid").val(id); // sets the file id, but wrongly appends
            $("#fileDisplayText").val(name); //sets the display name
            $("#mySelectFile").modal("hide"); //hides the modal file dialog
        });
    })
</script>

The value being set in the hidden field is the internal id of my file (database id), and the filename is for display.

My problem is that when i repeatedly select a new file, post the form and then selct and post then form etc. etc. Then the value from the file select is appended to the forms collection, so that when i post the form i get something like: 176, 178, 179, as the values, instead of just 176 which is the latest value I have selected.

*edit: markup added * This is the markup, please ignore the Razor variables, i named the id's of the input fields to match the javascript, in my code they are generated server side:

    <div class="templateSetting">
        <div>
            <span>
            <strong>
                @setting.SettingData.Name
                </strong>
            </span>        <br />
            <span>@setting.SettingData.Description</span>

            <input type="hidden" id="filehiddenid" name="setting_@setting.SettingData.Alias" value="@setting.Value" />
        </div>
        <div>
            <div style="float: left; width: 40%;">
<input type="text" style="width:545px;" id="fileDisplayText" name="meta_setting_@setting.SettingData.Alias" value="@fileName" class="form-control" /></div>
            <div style="width: 60%;">
                <a href="#" data-toggle="modal" title="Select File" data-target="#mySelectFile" class="btn btn-sm btn-default">Select File</a>
            </div>
            <div class="templatealias">@setting.SettingData.Alias</div>
            <div style="clear:both;"></div>
        </div>
    </div>

edit: This the entire markup, rendered:

<form action="/InteractiveApplications/EditApplication/23" id="editform" method="post" name="editform">
   <section id="container">
      <div id="wrapping" class="clearfix" style="padding-left:10px;width:100%;">
         <div id="WorkContent">
            <div class="validation-summary-valid" data-valmsg-summary="true">
               <ul>
                  <li style="display:none"></li>
               </ul>
            </div>
            <input data-val="true" data-val-number="The field CurrentFolderId must be a number." data-val-required="Feltet CurrentFolderId skal udfyldes." id="CurrentFolderId" name="CurrentFolderId" type="hidden" value="0">
            <input data-val="true" data-val-number="The field ApplicationId must be a number." data-val-required="Feltet ApplicationId skal udfyldes." id="ApplicationId" name="ApplicationId" type="hidden" value="23">
            <table style="width: 50%;padding-left: 20px;" class="tablelist">
               <tbody>
                  <tr>
                     <td valign="top">
                        <table width="100%">
                           <tbody>
                              <tr>
                                 <td style="width:120px;vertical-align:top;">
                                    <label for="Name">Name</label>:
                                 </td>
                                 <td>
                                    <input class="form-control" id="Name" name="Name" type="text" value="Afstemning">
                                 </td>
                              </tr>
                              <tr>
                                 <td style="width:120px;vertical-align:top;">
                                    <label for="Description">Description</label>:
                                 </td>
                                 <td>
                                    <textarea class="form-control" cols="20" id="Description" name="Description" rows="2">This is a poll to take when entering the library</textarea>
                                 </td>
                              </tr>
                              <tr>
                                 <td style="width:120px;vertical-align:top;">
                                    <label for="Templates">Template</label>:
                                 </td>
                                 <td>
                                    Simple Poll Template
                                 </td>
                              </tr>
                              <tr>
                                 <td style="width:120px;vertical-align:top;">
                                    <label for="ApplicationData">Path</label>:
                                 </td>
                                 <td>
                                    <input class="form-control" id="ApplicationData" name="ApplicationData" type="text" value="http://10.0.0.44:81/">
                                 </td>
                              </tr>
                              <tr>
                                 <td style="width:120px;vertical-align:top;">
                                    <label for="BlockHostExit">Block host exit</label>:
                                 </td>
                                 <td>
                                    <input checked="checked" data-val="true" data-val-required="Feltet Block host exit skal udfyldes." id="BlockHostExit" name="BlockHostExit" type="checkbox" value="true"><input name="BlockHostExit" type="hidden" value="false">
                                 </td>
                              </tr>
                           </tbody>
                        </table>
                     </td>
                     <td valign="top" style="padding-left: 20px">
                        <img src="/InteractiveImages/689a492e-7e01-49cc-b0b3-57e23b621706.jpg" width="100%" style="text-align: center;max-width: 200px;"><br>
                        <input id="ImagePath" name="ImagePath" type="hidden" value="/InteractiveImages/689a492e-7e01-49cc-b0b3-57e23b621706.jpg">                            <a style="color: gray" href="/InteractiveApplications/ChangePicture/23">
                        Change Picture
                        </a>
                     </td>
                  </tr>
               </tbody>
            </table>
         </div>
      </div>
   </section>
   <div class="titleBar" style="padding-left:-10px;height:30px;border-bottom:1px solid #307AAB">
      <h2> Application configuration</h2>
   </div>
   <div style="padding: 10px">
      <div>
         <div id="templateSettings" class="templateSettings">
            <div class="templateSetting">
               <div class="templateSettingLeading">
                  <strong>
                  Submit Text
                  </strong><br>
                  This is the text for the submit button
               </div>
               <div class="templateSettingInput">
                  <div style="width:50%;float:left;"> <input type="text" name="setting_submittext" value="qwer" class="form-control"></div>
                  <div class="templatealias">submittext</div>
                  <div style="clear:both;"></div>
               </div>
            </div>
         </div>
         <div id="templateSettings" class="templateSettings">
            <div class="templateSetting">
               <div class="templateSettingLeading">
                  <strong>
                  Multiple choice
                  </strong><br>
                  This determines if the poll has multiple right answers
               </div>
               <div class="templateSettingInput">
                  <input type="hidden" id="setting_ismultiplechoice" name="setting_ismultiplechoice" value="off">
                  <input id="ismultiplechoice" type="checkbox" name="setting_ismultiplechoice">
                  <div class="templatealias">
                     ismultiplechoice
                  </div>
               </div>
            </div>
            <script>
               $(document).ready(function () {
                   $("#checkbox_ismultiplechoice").click(function () {
                       var settingId = "#setting_ismultiplechoice";
                       var currentVal = $(settingId).val();
                       if (currentVal == "off") {
                           $(settingId).val("on");
                       }
                       else {
                           $(settingId).val("off");
                       }
                   });
               });
            </script>
         </div>
         <div id="templateSettings" class="templateSettings">
            <script type="text/javascript">
               var answerCounter = 1;
               var trueFalseCounter = 0;
               $(document).ready(function () {
                   $("#addbutton").click(function () {
                       answerCounter += 1;
                       trueFalseCounter += 1;
                       $("#questioninput_question").append("<div id='answer_" + answerCounter + "'>Answer: <input type='text' name='setting_question:" + answerCounter + "' value='' class='form-control' /><input type='checkbox' name='answer_setting_question:" + answerCounter + "' />This is the correct answer    <input type='button' value='Delete' class='deleteButton btn btn-warning btn-xs' id='deletebutton_" + answerCounter + "'/></div>");
                   });

                   $(".templateSettingInput").on("click", ".deleteButton", function () {
                       if(confirm("Are you sure you want to delete this answer?"))
                       {
                           $(this).parent().remove();
                       }

                   });
               });
            </script>
            <div class="templateSetting">
               <div class="templateSettingLeading">
                  <strong>  Question</strong>
                  This is the question and the answers for the poll
               </div>
               <div class="templatealias">
                  question
               </div>
               <div class="templateSettingInput" id="questioninput_question" style="width:50%;">
                  <textarea cols="40" rows="4" name="setting_question" class="form-control">werwe</textarea>
                  <input style="vertical-align: top" type="button" id="addbutton" value="Add answer" class="addbutton btn btn-info btn-xs navbar-btn"><br>
                  <div id="answer_1">
                     Answer:
                     <input type="text" name="setting_question:1" value="wer" class="form-control"> 
                     <!--<input type='checkbox'  name='answer_setting_question:1' />This
                        is the correct answer-->
                     <input type="button" value="Delete" id="deletebutton_1" class="deleteButton btn btn-warning btn-xs">
                  </div>
               </div>
            </div>
         </div>
         <div id="templateSettings" class="templateSettings">
            <div class="templateSetting">
               <div class="templateSettingLeading">
                  <strong>
                  Show Answer to user
                  </strong><br>
                  determines if the user should see answers
               </div>
               <div class="templateSettingInput">
                  <input type="hidden" id="setting_showanswer" name="setting_showanswer" value="off">
                  <input id="showanswer" checked="'checked'" type="checkbox" name="setting_showanswer">
                  <div class="templatealias">
                     showanswer
                  </div>
               </div>
            </div>
            <script>
               $(document).ready(function () {
                   $("#checkbox_showanswer").click(function () {
                       var settingId = "#setting_showanswer";
                       var currentVal = $(settingId).val();
                       if (currentVal == "off") {
                           $(settingId).val("on");
                       }
                       else {
                           $(settingId).val("off");
                       }
                   });
               });
            </script>
         </div>
         <div id="templateSettings" class="templateSettings">
            <div class="templateSetting">
               <div>
                  <span>
                  <strong>
                  Background
                  </strong>
                  </span>        <br>
                  <span>Please select a background image</span>
                  <input type="hidden" id="id_background" name="setting_background" value="172,201,173,175,172,178,178,,">
               </div>
               <div>
                  <div style="float: left; width: 40%;"><input type="text" style="width:545px;" id="text_background" name="meta_setting_background" value="611a756c4c3d338fc4ffcebf8b1979d6.png" class="form-control"></div>
                  <div style="width: 60%;">
                     <a href="#" data-toggle="modal" title="Select File" data-target="#mySelectFile" class="btn btn-sm btn-default">Select File</a>
                  </div>
                  <div class="templatealias">background</div>
                  <div style="clear:both;"></div>
               </div>
            </div>
            <!-- Modal -->
            <div class="modal fade" id="mySelectFile" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
               <div class="modal-dialog">
                  <div class="modal-content">
                     <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel">Select file</h4>
                     </div>
                     <div class="modal-body">
                        <div class="foldertree">
                           <ul>
                              <li id="0" class="folder">
                                 <a href="#" class="folderSelect" id="0">All</a>
                                 <ul>
                                    <li id="173" class="file"><a href="#" class="fileSelect" id="173">iStock_000032787140Large.jpg</a></li>
                                    <li id="174" class="file"><a href="#" class="fileSelect" id="174">52_fordele.jpg</a></li>
                                    <li id="175" class="file"><a href="#" class="fileSelect" id="175">11376047043_a06bed34bd_o.jpg</a></li>
                                    <li id="177" class="file"><a href="#" class="fileSelect" id="177">DigitalSignage.png</a></li>
                                    <li id="178" class="file"><a href="#" class="fileSelect" id="178">mediawall_search_br.jpg</a></li>
                                 </ul>
                              </li>
                              <ul>
                                 <li id="59" class="folder">
                                    <a href="#" class="folderSelect" id="59">Test interactive folder</a>
                                    <ul>
                                       <li id="172" class="file"><a href="#" class="fileSelect" id="172">611a756c4c3d338fc4ffcebf8b1979d6.png</a></li>
                                    </ul>
                                 </li>
                              </ul>
                           </ul>
                        </div>
                     </div>
                     <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                     </div>
                  </div>
                  <div class="templateSettingInput">
                     <input type="text" name="setting_background" value="172,201,173,175,172,178,178,," class="form-control">
                     <div class="templatealias">background sdhsdsd</div>
                  </div>
               </div>
               <script>
                  $(function () {
                      $(".fileSelect").click(function () {
                          var name = $(this).text();
                          var id = $(this).attr("id");
                          var elementId = 'id_background';
                          var elementText = 'text_background';            
                          $("#" + elementId).val(id);
                          $("#" + elementText).val(name);
                          $("#mySelectFile").modal("hide");
                      });
                  })
               </script>
            </div>
         </div>
      </div>
   </div>
</form>
Brian Hvarregaard
  • 4,081
  • 6
  • 41
  • 77
  • 1
    is this block of the code the only code that acts on your form elements? can we see your form html? – Ji_in_coding Jan 22 '15 at 14:37
  • As written, your code does replace the id rather than append: http://jsfiddle.net/aybqjh18/ Whatever's going wrong is, I'm guessing, somewhere in your form post loop – Daniel Beck Jan 22 '15 at 14:40
  • do you have duplicate ids on any of your html elements? – thelastshadow Jan 22 '15 at 14:41
  • If i make an alert($("#filehiddenid").val()) then it shows only one value – Brian Hvarregaard Jan 22 '15 at 14:46
  • That last comment is in conflict with question. Seems like your problem is server side – charlietfl Jan 22 '15 at 14:56
  • I second @DanielBeck, I suspect it is something in posting, if you are using $.post, can you console.log(data) before calling it? (assuming data is what is being posted) – Sherif Jan 22 '15 at 15:00
  • My problem is that i cant see where im getting the error. Server side i have ASP.NET MVC and posts the entire form. My collection of elements holds a hidden field with the correct ID, with multiple values with my most recent value insert at the start of the string. And thats when i inspect it immediately on server side, without handling it in any way. It is as if the "form" holds a list of fields and somehow adds to the collection every time. – Brian Hvarregaard Jan 22 '15 at 15:10
  • GOT IT: In my modal i was actually rendering a field wich had the same id as the one i was submitting. This caused the problem. Had to serialize the form to see the result before i was submitting it, to see that there was two fields with the same name. Please add answers to close this embarassing issue :-) – Brian Hvarregaard Jan 22 '15 at 15:25

1 Answers1

2

Make sure there aren't other hidden inputs on the page with the same id and view the html in your browser and verify that the value on the element is correct before jquery modifies it.

The jquery id selector (http://api.jquery.com/id-selector/) will only select the first element in the DOM that has that id.

If there are multiple hidden input elements with the same name on the form, they will all get posted to the server.

Edward
  • 3,292
  • 1
  • 27
  • 38
Evan OJack
  • 567
  • 2
  • 5
  • Edited to provide information that is more of an answer. – Evan OJack Jan 22 '15 at 14:59
  • Well, it's more of an answer now, I can accept that, but I don't think this is the particular issue the asker is facing :P – Tim Lewis Jan 22 '15 at 15:02
  • I edited my answer to clarify 'If there are multiple hidden input elements with the same name on the form, they will all get posted to the server', which turned out to be the problem. – Evan OJack Jan 22 '15 at 15:56