简体   繁体   English

HTML addEventListener select-onchange或onselect

[英]Html addEventListener select - onchange or onselect

Looked at a few questions but did not find exact event name. 看了几个问题,但没有找到确切的事件名称。

Have handle to a select box inside a frame (frame source is same domain). 对框架内的选择框有句柄(框架源是同一域)。

Can see the inner HTML of the select but not sure how to add a onchange/ on selection change of the multi select. 可以看到选择的内部HTML,但不确定如何在多选择的选择更改中添加onchange /。

 <select multiple="" size="3"><option value="12.5">12.5</option>
    <option value="12.678">12.678</option><option value="14.333">14.333</option>
    <option value="521.991">521.991</option><option value="221.976">221.976</option>      
 </select>

Above is the outer html I see for the select. 上面是我选择的外部HTML。

Trying to add event by 尝试通过添加事件

co1c.addEventListener('change',  f1s1onChange, false);

Is there an on select event? 有选择事件吗? What text to use for that in addEventListener param 1? addEventListener参数1中该使用什么文本?

In the iframe have : 在iframe中有:

onload="hookLoadDivInfo(this);" 

and For debug 和用于调试

<div id="cc">
Debug will go here</div>

All js in parent page: 父页面中的所有js:

 <script>


function chk(){
    hookLoadDivInfo2(2)
    f1s1onChange()
}

function hookLoadDivInfo(frameObj){
 //alert(2)
 window.frame1 = frameObj;
 setTimeout('hookLoadDivInfo2(1)', 1600);
    //alert(2)
}
function hookLoadDivInfo2(p1){
    //debugging - log
    frameObj = window.frame1
    c1 = document.getElementById("cc")

    c1.innerHTML = ''  + "DEBUG INFO  1 : "
    co1 = frameObj.contentWindow.document.getElementById("widgetContainer-content-pov_widget_2")

    co1.style.display = 'none'

    co1 = frameObj.contentWindow.document.getElementById("widgetContainer-content-pov_widget_1")
    c1.innerHTML = c1.innerHTML  + "<br> <pre>" + co1 + "</pre><br>"
    x = co1.getElementsByTagName('SELECT')
    //alert('D ' + co1.innerHTML + ' c' + x.length)
    co1c = x.item(0)
    //alert('D ' + co1c.outerHTML)
    c1.innerHTML = c1.innerHTML  + "<br> <pre>" + co1c + "</pre><br>"


    window.f1s1 = co1c 
            if(p1==1){
        co1c.addEventListener('change',  f1s1onChange, false);
    }
    c1.innerHTML = c1.innerHTML  + "<br>Done z co1c: " + window.f1s1

}

function f1s1onChange(){
   c1 = document.getElementById("cc")
   c1.innerHTML =  "DEBUG  6 f1s1onChange:" + window.f1s1 + " " + window.f1s1.length + " " + new Date()

  for( i =0; i < window.f1s1.size; i++){
    c1.innerHTML =  c1.innerHTML + "<br> [" + i + " " + window.f1s1.options[i].selected
  } 

}



</script>

This does not throw errors but dont see correct selected items either? 这不会引发错误,但是也看不到正确的所选项目吗?

Was using .size instead of .length and the list was more than 3 elements and had selected bottom item so was showing as not selected 正在使用.size而不是.length,并且该列表包含3个以上元素,并且已选择底部项目,因此显示为未选中

<input type=button onclick=chk() value=Both> <input type=button onclick=f1s1onChange() value=f1s1onChange>
<input type=button onclick=hookLoadDivInfo2(2) value=hookLoadDivInfo2>

<div id="cc">
For debug</div>

<script>


function chk(){
    hookLoadDivInfo2(2)
    f1s1onChange()
}

function hookLoadDivInfo(frameObj){
 //alert(2)
 window.frame1 = frameObj;
 setTimeout('hookLoadDivInfo2(1)', 2600);
    //alert(2)
}
function hookLoadDivInfo2(p1){
    //debugging - log
    frameObj = window.frame1
    c1 = document.getElementById("cc")

    c1.innerHTML = ''  + "DEBUG INFO  1 : "
    co1 = frameObj.contentWindow.document.getElementById("widgetContainer-content-pov_widget_2")

    co1.style.display = 'none'

    co1 = frameObj.contentWindow.document.getElementById("widgetContainer-content-pov_widget_1")
    c1.innerHTML = c1.innerHTML  + "<br> <pre>" + co1 + "</pre><br>"
    x = co1.getElementsByTagName('SELECT')
    //alert('D ' + co1.innerHTML + ' c' + x.length)
    co1c = x.item(0)
    //alert('D ' + co1c.outerHTML)
    c1.innerHTML = c1.innerHTML  + "<br> <pre>" + co1c + "</pre><br>"


    window.f1s1 = co1c 
            if(p1==1){
        co1c.addEventListener('change',  f1s1onChange, false);
    }
    c1.innerHTML = c1.innerHTML  + "<br>Done z co1c: " + window.f1s1

}

function f1s1onChange(){
   c1 = document.getElementById("cc")
   c1.innerHTML =  "DEBUG  6 f1s1onChange:" + window.f1s1 + " " + window.f1s1.length + " " + new Date()

  for( i =0; i < window.f1s1.length; i++){
    c1.innerHTML =  c1.innerHTML + "<br> [" + i + " " + window.f1s1.options[i].selected + " " + window.f1s1.options[i].text
  } 

}



</script>

Try something like this : 尝试这样的事情:

 $(document).on('click keypress', 'select', function (e) {
   //do Something
 });

Or add a onClick to the select tag 或者将onClick添加到选择标签

first of all, When you are calling the check Function? 首先,当您调用检查功能时? You can capture the event at JQuery you must set an ID for <select> 您可以在JQuery捕获事件,必须为<select>设置ID

$("select#selectmenu1").change(function() {


}

I mean with that code you can capture the event, maybe you want to save the selected option on a var or fire another function if you want get the selected value you can try 我的意思是,使用该代码,您可以捕获事件,也许您想将选定的选项保存在var上,或者触发另一个函数,如果要获得选定的值,可以尝试

var varName= $("select#selectmenu1"); 
varName = (varName[0].value);
alert(varName);

Notice at the first line varName is an object and at the second line varName is a String 请注意,第一行varName是一个对象,第二行varName是一个String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM