使用jquery设置选择值

时间:2017-05-06 15:43:50

标签: javascript jquery select

基督是复活的,

您好,

我有以下代码:



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

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>

我正在尝试设置值。 $(&#34;#grammatoseires&#34)VAL(gramatoseira);

以上代码不会做任何事情。

任何帮助都是有用的。

3 个答案:

答案 0 :(得分:1)

没有必要用JS做这个。只需直接输出选项:

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

答案 1 :(得分:1)

它不喜欢您的选项值中的转义引号。

执行:

<option value="'Lucida Console', Monaco, monospace">...</option>

<option value="&#39;Lucida Console&#39;, Monaco, monospace">...</option>

$('button').click( function () {
  gramatoseira = "'Lucida Console', Monaco, monospace";
  $("#grammatoseires").val(gramatoseira);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></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>
<button>Set it</button>

答案 2 :(得分:0)

基于Jonas W代码。

我写了这个:

    <?php
        $fonts = array();
        $fonts[0] = "Georgia, serif";
        $fonts[1] = "\'Palatino Linotype\', Book Antiqua, Palatino, serif";
        $fonts[2] = "\'Times New Roman\', Times, serif";
        $fonts[3] = "\'Arial Black\', Helvetica, sans-serif";
        $fonts[4] = "\'Comic Sans MS\', cursive, sans-serif";
        $fonts[5] = "\'Lucida Sans Unicode\', Lucida Grande, sans-serif";
        $fonts[6] = "Tahoma, Geneva, sans-serif";
        $fonts[7] = "\'Trebuchet MS\', Helvetica, sans-serif";
        $fonts[8] = "Verdana, Geneva, sans-serif";
        $fonts[9] = "\'Courier New\', Courier, monospace";
        $fonts[10] = "\'Lucida Console\', Monaco, monospace";
        $font_output = "";
        foreach($fonts as $font){
            $font_output.="<option value=\"".$font."\"";
            if(isset($_COOKIE["gramatoseira"])&& $font==$_COOKIE["gramatoseira"]) {
                $font_output.=" selected='selected' ";
            }
            $font_output.=">".$font."</option>";
        }
        echo $font_output;
    ?>

效果很好。 谢谢大家的帮助!