[英]onchange event not firing on first change in javascript?
<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>
Thank you for taking the time for reading my question. 感谢您抽出宝贵时间阅读我的问题。
onchange()
is not working for the first time second time it works fine onchange()
第一次无法正常工作,第二次正常
Remove onchange
from select
box. 从
select
框中删除onchange
。 And change your JS with this : 并通过以下方式更改您的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>
Very easy and simple code below 下面的代码非常简单
<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 javascript功能
function getIPL(value) {
//var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' );
//urlmenu.onchange = function() {
window.open( 'admin_mailbox.php?Id=' + value,"_self");
//};
}
When you are using document.getElementById
for getting element id, than no need to use onchange()
event in <select>
当您使用
document.getElementById
获取元素ID时,无需在<select>
使用onchange()
事件。
And i also suggest you to add one more option for selection <option>Select</option>
而且我还建议您为选择
<option>Select</option>
添加更多选项
Test Example (change as per your PHP Code): 测试示例(根据您的PHP代码进行更改):
var urlmenu = document.getElementById( 'kUI_dropdown_basic_select' ); urlmenu.onchange = function() { window.open( 'admin_mailbox.php?Id=' + this.options[ this.selectedIndex ].value,"_self"); };
<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>
You have already defined onchange event of select box here...then why you use onchange again on same element. 您已经在此处定义了选择框的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.