2016-08-22 80 views
-2

当javascript onchange中的值内部回显不起作用时。它必须再次点击/选择以显示div。选择选项值时显示/隐藏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?

+0

在选项值至少为2倍的值的功能活性()来行动。 –

+0

对我来说,逻辑应该是'if(this.selectedIndex> 0)...'那么你可以有无限数量的选项,并且只有在选择了第一个选项以外的选项时才显示div(假设第一个('select ...'),或者甚至是$('#reqtypev')。change(function(){$('#otherTypev')[this.selectedIndex> 0?'show':'隐藏']()})'。 – RobG

回答

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)); 
+0

感谢这一点,它必须触发显示div。很有帮助。 –

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() 
    } 
}); 
0

嗯,我不认为这是可行的PHP,PHP执行一次和退出(直到另一个刷新时)

你可以做到这一点在PHP中,一次。但不是人们移动选择栏的地方。

你可以把你想要做的更多的信息?我想从你展示一些代码是隐藏字段的形式一个人一旦页面加载后选择一个值< 1> 4

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() 
    } 
} 
0

价值OPTIO ■至少2个值的功能变化()到活动

<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>