简体   繁体   中英

Syntax Error on Select and Option Tag

So I have this code in me. I used an option tag that will echo all of the database's value in a select tag (drop down menu). What I want is that upon selecting an item, I want it to stay in the dropdown menu until I choose another one. So the only applicable answer is to add an attribute to the option tag which is selected="selected" . Right? I somehow get a syntax error on placing the quotations. I know this is a noob question. Apparently I'm training myself to PHP. Could you please help me with this? Here is my code:

echo "<option value = '".$row['brandname']."'>" .$row['brandname'] . "</option>";

So where should I place the selected attribute there? Any help? By the way, I'm using the onchange ="this.form.submit()" .

将代码更改为此:

echo "<option selected value = '".$row['brandname']."'>" .$row['brandname'] . "</option>";

echo "<option value = '".$row['brandname']."' selected='selected'>" .$row['brandname'] . "</option>";

or you can selects options by JQuery after completion of running PHP script at client side

your php output .html file

<select class='list_brand'> <?php foreach($brandobj as $row){ echo "<option value = '".$row['brandname']."'>" .$row['brandname'] . "</option>"; } ?> </select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script> $('.list_brand option[value='brand-to-select']).attr('selected','selected'); </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM