php jquery selected
I having long list of select options predefined.
So i tried something like this when i try to edit the content
<body>
<select id="test" ....
<option value='99'>99</option>
</select>
<script language="javascript">
$(document).ready(function () {
$("select#test option[value='<?php echo $row['test']; ?>'").attr("selected", "selected");
});
</script>
</bod开发者_开发百科y>
php value is 99 Why this isn't working? Anything i did wrong?
You are lacking a ]
[value='<?php echo $row['test']; ?>'
You are missing the closing ']' in your selector. Change it to:
$("select#test option[value='<?php echo $row['test']; ?>']").attr("selected", "selected");
Aliter
Another way to select a value for select element is to use the val i.e
$("select#test").val('<?php echo $row['test']; ?>');
This is usually how I do it:
<select id="test">
...
<option value="99">99</option>
...
</select>
<script>$("#test.val("<?php echo $row['test']; ?>");</script>
Basically, setting the value
of the <select>
element chooses the right option for you.
精彩评论