1

I have a problem with the multi-select box. I tried to select multiple values using jQuery, but only the last one is selected, can anyone help me please? Here is my code:

<script>
$(function(){
 <?php 
 foreach ($selectdefaults as $s):
   ?>   
   $('#q<?php echo $s['campsetjoin']['campid'];?> select').val('<?php echo $s['campsetjoin']['setid'];?>');
   <?php  
 endforeach;
 ?> 
});
</script>

Here is my HTML source (this is generated with PHP):

<table>
<tr>
    <td>
        <form id="s1" name="camps">
            <input type="hidden" name="camid" value="1" />
            <strong> Camp 1 </strong>
        </form>
    </td>
    <td align="right" id="q1">
        <select name="qsets" multiple="multiple">
            <option value="1">common set</option>
            <option value="2">test set</option>
        </select>
    </td>
    <td>
        <input type="button" value="update" cid="1" class="btncdsave" />
    </td>
</tr>
<tr>
    <td>
        <form id="s9" name="camps">
            <input type="hidden" name="camid" value="9" />
            <strong> Camp 2 </strong>
        </form>
    </td>
    <td align="right" id="q9">
        <select name="qsets" multiple="multiple">
            <option value="1">common set</option>
            <option value="2">test set</option>
        </select>
    </td>
    <td>
        <input type="button" value="update" cid="9" class="btncdsave" />
    </td>
</tr>
<tr>
    <td>
        <form id="s10" name="camps">
            <input type="hidden" name="camid" value="10" />
            <strong> Camp 3 </strong>
        </form>
    </td>
    <td align="right" id="q10">
        <select name="qsets" multiple="multiple">
            <option value="1">common set</option>
            <option value="2">test set</option>
        </select>
    </td>
    <td>
        <input type="button" value="update" cid="10" class="btncdsave" />
    </td>
</tr>
</table>

So I need to select multiple values of the select box. I can get the form id and select box value ids for selection. Thanks for you help.

1 Answer 1

1

If you want to select multiple values, pass an array with those values

 $('#q10 select').val(['1', '2']);

fiddle here http://jsfiddle.net/p8R4k/

Sign up to request clarification or add additional context in comments.

3 Comments

this is correct but my loop returns $('#q9 select').val('2'); $('#q10 select').val('2'); $('#q10 select').val('1'); have any suggestions for that ?
@Suneth you should change the logic of your loop so that it outputs this javascript code, i just pointed out how to select multiple elements in a multiselect. :)
@Suneth it depends on whar $selectdefaults holds, i'd suggest you to open a new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.