Solved

jQuery UI Selectable form values

Posted on 2013-02-07
1
445 Views
Last Modified: 2013-02-11
I am using the jQuery UI selection box to create a multiple choice form.
The question and its options are created dynamically from mysql.
Therefore there could be 2 options to choose from or 22.

I want each selection to be entered into the database separately.

As you can see, currently I am inserting the values in 1 textbox, seperating each number via a comma.

What I want is to have a hidden textbox for each option. Then if the selection is selected, the value is inserted into its corresponding textbox.

Code i am currently using below...



jQuery
    $(function() {
        $("#selectable").bind("mousedown", function(e) {
            e.metaKey = true;
        }).selectable({
            stop: function() {
                var result = "";
                $( ".ui-selected", this ).each(function() {
                    var index = $( "#selectable li" ).index( this );
					
                    result += (( index + 1 ) + "," );
                });

				$("input#selectResult").val(result);
            }
        });
    });

Open in new window




HTML

<ul id="selectable">
<?php while($optionRow = mysql_fetch_array($optionResult))
{?>
      <input type="text" id="selectResult" name="<?php echo "qno".$questionID; ?>" value="" />
      <li id="<?php echo $optionRow['questionOptionID'] ?>" class="ui-state-default"><?php echo $optionRow['questionOption'] ?></li>
php } ?>
</ul>

Open in new window

0
Comment
Question by:Steve Tinsley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38866043
 $(function() {
        $("#selectable").bind("mousedown", function(e) {
            e.metaKey = true;
        }).selectable({
            stop: function() {
                $( ".ui-selected", this ).each(function(i, val) {
                    var index = $( "#selectable li" ).index( this );
  		    $("input:hidden.selectResult:eq(" + i + ")").val(index + 1);
                });
            }
        });
    });

Open in new window

With :
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />

Open in new window

0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question