onchange事件没有在javascript的第一次更改中触发?

时间:2016-02-25 05:52:39

标签: javascript php

<form class="uk-form-stacked">
    <label class="uk-form-label" for="kUI_dropdown_basic_select">Select a user</label>
    <select id="kUI_dropdown_basic_select" class="uk-form-width-medium" onchange="getIPL();">
        <?php  foreach ($response->result->allAcc as $data) { ?>
            <option  value="<?php echo $data->Id;?>">   <?php echo $data->Name; ?></option>
        <?php } ?>
    </select>
</form>


<script>
function getIPL() {
    var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
    urlmenu.onchange = function() {
    window.open( 'admin_mailbox.php?Id=' + this.options[ this.selectedIndex ].value,"_self");
    };
}
</script>

感谢您抽出宝贵时间阅读我的问题。 onchange()第一次无效 第二次它工作正常

4 个答案:

答案 0 :(得分:3)

onchange框中删除select。并用这个改变你的JS:

<script>

    var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
    urlmenu.onchange = function() {
    window.open( 'admin_mailbox.php?Id=' + this.options[ this.selectedIndex ].value,"_self");
    };

</script>

答案 1 :(得分:1)

下面非常简单易用的代码

<form class="uk-form-stacked">
   <label class="uk-form-label" for="kUI_dropdown_basic_select">Select a user</label>

   <select id="kUI_dropdown_basic_select" onmousedown="this.value='';" class="uk-form-width-medium" onchange="getIPL(this.value);">
      <option  value="aaa">aaa</option>
      <option  value="bbb">bbb</option>
      <option  value="ccc">ccc</option>
      <option  value="ddd">ddd</option>
   </select>
</form>

javascript功能

function getIPL(value) {
  //var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
  //urlmenu.onchange = function() {
  window.open( 'admin_mailbox.php?Id=' + value,"_self"); 
  //};
}

答案 2 :(得分:1)

当您使用document.getElementById获取元素ID时,不需要在onchange()中使用<select>个事件

我还建议您为选择<option>Select</option>

添加一个选项

测试示例(根据您的PHP代码进行更改):

&#13;
&#13;
var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
urlmenu.onchange = function() {
window.open( 'admin_mailbox.php?Id=' + this.options[ this.selectedIndex ].value,"_self");    	
};
&#13;
<form class="uk-form-stacked">
    <label class="uk-form-label" for="kUI_dropdown_basic_select">Select a user</label>
    <select id="kUI_dropdown_basic_select" class="uk-form-width-medium">
        <option value="">Select</option>
        <option value="test1">Test1</option>
        <option value="test2">Test2</option>
    </select>
</form>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

您已经在此处定义了选择框的onchange事件...然后为什么在同一元素上再次使用onchange。

使用以下代码

<form class="uk-form-stacked">
    <label class="uk-form-label" for="kUI_dropdown_basic_select">Select a user</label>
    <select id="kUI_dropdown_basic_select" class="uk-form-width-medium">
        <?php  foreach ($response->result->allAcc as $data) { ?>
            <option  value="<?php echo $data->Id;?>">   <?php echo $data->Name; ?></option>
        <?php } ?>
    </select>
</form>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
  $("#kUI_dropdown_basic_select").change(function(){
        window.open( 'admin_mailbox.php?Id=' + $(this).val() ,"_self");
  });
});
</script>