2016-08-22 82 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>