选择选项值时显示/隐藏Div

时间:2016-08-22 03:56:41

标签: javascript php

当回显内部值时,javascript onchange不起作用。它必须再次单击/选择以显示div。

<?php
$test = '1'; 
?>

    <select name="request" id="reqtypev" class="form-control1" >
    <option value="<?php echo $test ?>" selected="selected"><?php echo $test ?></option>
    </select>

如果您没有使用php回复它,它确实有用。

//javascript
      $('#reqtypev').change(function(){
        if( $(this).val()==="1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
        $("#otherTypev").show()
        } else {
         $("#otherTypev").hide()
        }
    });

当你使用php回音时怎么可能执行javascript?

5 个答案:

答案 0 :(得分:1)

#reqtypev页面加载后触发更改事件如下:

(function ($) {

    $(function () {
        $('#reqtypev').change(function(){
            if( $(this).val()==="1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
                $("#otherTypev").show()
            } else {
                $("#otherTypev").hide()
            }
        });


        $('#reqtypev').trigger("change");
    });

}(jQuery));

答案 1 :(得分:1)

试试这个:

<?php
$test = '1'; 
?>

    <select name="request" id="reqtypev" class="form-control1" onchange="intializeDrop(this.value);" >
    <option value="<?php echo $test ?>" selected="selected"><?php echo $test ?></option>
    </select>

Js代码:

function intializeDrop(id){
    if( $(this).val()==="1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
        $("#otherTypev").show()
    } else {
        $("#otherTypev").hide()
    }
}

答案 2 :(得分:0)

试试这个

//javascript
  $(document).on('change','#reqtypev',function(){
    if( $(this).val()==="1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
    $("#otherTypev").show()
    } else {
     $("#otherTypev").hide()
    }
});

答案 3 :(得分:0)

嗯,我不认为它在PHP中可行,PHP执行一次并退出(直到另一次刷新发生)

你可以用PHP做一次。但不适合人们移动选择栏的地方。

你能提供更多关于你想做什么的信息吗?我假设你所展示的小代码是在一个人选择一个值时隐藏表格中的一个字段。 1或&gt; 4

答案 4 :(得分:0)

选项中的值至少为2个值,函数change()为activity

<div id="otherTypev">texttttt</div>
<select name="request" id="reqtypev" class="form-control" >
  <?php 
    for($i = 0;$i<=10;$i++){
      echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
    }
  ?>  
</select>

的js

<script>
   $('#reqtypev').change(function(){
    if( $(this).val()=== "1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
       $("#otherTypev").show()
    } else {
      $("#otherTypev").hide()
    }
 });
</script>
相关问题