Heim  >  Fragen und Antworten  >  Hauptteil

javascript – So wählen Sie verschiedene Optionswerte basierend auf den Werten aus, die aus dem Hintergrund in HTML Select übergeben werden

Code:

 <tr>
    <th>空间性质</th>
     <td>
         <input type="hidden" id = "class" value="{$post.post_class}"/>

         <select class="form-control" name="post[post_class]" id="class2" value="{$post.post_class}">
         <option value="0" id="op1">出售</option>
         <option value="1" id="op2">出租</option>
         </select>
         </td>
 </tr>

zeigt unterschiedliche Optionswerte entsprechend dem Wert von value={$post.post_class} an, und der Wert hat nur zwei Werte ​​0 und 1. TKS

淡淡烟草味淡淡烟草味2672 Tage vor1186

Antworte allen(5)Ich werde antworten

  • 黄舟

    黄舟2017-06-28 09:27:35

    默认选择是吧,用jquery的attr就可以了,假设默认选择值为1的选项,代码如下:

    $("#class option[value='1']").attr('selected',true);

    Antwort
    0
  • 巴扎黑

    巴扎黑2017-06-28 09:27:35

    将select标签中的value置为0 或 1 不就可以了吗

    Antwort
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-28 09:27:35

    $("#class option[value='1']").attr('selected',true);
    或
    $("#class").val(1);

    Antwort
    0
  • 世界只因有你

    世界只因有你2017-06-28 09:27:35

    http://jsrun.net/d9YKp

    Antwort
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-28 09:27:35

    由于:document.querySelector('#class').value获取不到select中的value值(即<select class="form-control" name="post[post_class]" id="class2" value="{$post.post_class}">)。

    所以加一个隐藏的input <input type="hidden" id = "class" value="{$post.post_class}"/>来获取后台传来的值,然后再判断。

    <script type="text/javascript">
        var sv = document.getElementById('class').value;
        if(sv == 0){
            $("#class2 option[value='0']").attr('selected',true);
        }else {
            $("#class2 option[value='1']").attr('selected',true);
        }
    </script>

    Antwort
    0
  • StornierenAntwort