How to display checked value in jQuery unpack list populated from database

How to display marked / verified data in a jQuery dropdown. Basically my problem is that I want to display the back checked value in the jquery dropdown that is stored in the database.

I tried using the attr selected in my javascript, something like this.

<script type="text/javascript"> $(document).ready(function() { $("#<?php echo $bnum; ?>").dropdownchecklist({width: 300}); //the option value suppose to be dynamic,but for the purpose of this question, the value will be someothing like this. $("#<?php echo $bnum; ?> option[value='2']").attr("selected", true); }); </script> 

The value for the checkbox that I am extracting from the sql procedure looks something like this:

For room 501: 2

What I'm trying to do now is when a user clicks on a page. The drop-down list checklist is marked with a value retrieved from the database.

Below is the code for the room rows that contains jQuery for each row

 <select id="<?php print $bnum; ?>" multiple="multiple" name="status[]" > <option value="0"></option> <?php //query drop-down list $sqlakh = "select diet_id,diet_name from bmsdb.diettypes"; $resultakh = mysql_query($sqlakh); while ($rowsakh = mysql_fetch_array($resultakh)) { ?> <option value='<?php echo $rowsakh['diet_id'] . '|' . $bnum; ?>' "selected" : ''> <?php echo $rowsakh['diet_name']; ?></option> <?php } ?> </select> 

Thanks in advance, im new in javascript. Can someone help me find out how to display the marked item in the jQuery dropdown that is retrieved from the database.

+4
source share
1 answer

you can address this question: jQuery - setting the selected value of the select control through its text description

But if you use php to build the html option, why not set the selected option using php. Something like this in your loop (just an example!)

 <?php $sel = ''; if($rowsakh['diet_id'] == $_POST['someVarName']) { $sel = 'selected'; } echo ' <option value="' . $rowsakh['diet_id'] . '|' . $bnum . '"' . $sel . '>Audi</option>'; ?> 

I personnaly would not use echo commands in a while loop. I would tie it up as a long string and finally an echo once at the end.

0
source

Source: https://habr.com/ru/post/1484112/


All Articles