基督复活了,

你好,

我有以下代码:



<script type="text/javascript">
			gramatoseira = "Tahoma, Geneva, sans-serif";
			<?php
			if(isset($_COOKIE["gramatoseira"])) {
				echo "gramatoseira = \"" . $_COOKIE["gramatoseira"]."\";";
			}
			?>
</script>





php执行输出类似于:

<script type="text/javascript">
            gramatoseira = "Tahoma, Geneva, sans-serif";
            gramatoseira = "\'Lucida Console\', Monaco, monospace";     </script>


<select id="grammatoseires">
        <option value="Georgia, serif">Georgia, serif</option>
        <option value="\'Palatino Linotype\', Book Antiqua, Palatino, serif">"Palatino Linotype", "Book Antiqua", Palatino, serif</option>
        <option value="\'Times New Roman\', Times, serif">"Times New Roman", Times, serif</option>
        <option value="\'Arial Black\', Helvetica, sans-serif">"Arial Black", Gadget, sans-serif</option>
        <option value="\'Comic Sans MS\', cursive, sans-serif">"Comic Sans MS", cursive, sans-serif</option>
        <option value="\'Lucida Sans Unicode\', Lucida Grande, sans-serif">"Lucida Sans Unicode", "Lucida Grande", sans-serif</option>
        <option value="Tahoma, Geneva, sans-serif">Tahoma, Geneva, sans-serif</option>
        <option value="\'Trebuchet MS\', Helvetica, sans-serif">"Trebuchet MS", Helvetica, sans-serif</option>
        <option value="Verdana, Geneva, sans-serif" >Verdana, Geneva, sans-serif</option>
        <option value="\'Courier New\', Courier, monospace">"Courier New", Courier, monospace</option>
        <option value="\'Lucida Console\', Monaco, monospace">"Lucida Console", Monaco, monospace</option>
    <select>


我正在尝试设置值。
$(“#grammatoseires”)。val(gramatoseira);

上面的代码什么都不做。

任何帮助将是有用的。

最佳答案

无需使用JS执行此操作。只需直接输出选项:

 <option value="Georgia, serif">Georgia, serif</option>
 <?php
        if(isset($_COOKIE["gramatoseira"])) {
            echo "<option selected='selected' value='" . $_COOKIE["gramatoseira"]."' >".$_cookie["gramatoseira"]."</option>";
        }
?>

关于javascript - 用jQuery设置选择值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43822333/

10-12 13:40