简体   繁体   English

HTML onclick document.getElementById

[英]HTML onclick document.getElementById

I have a question which I am sure is simple but I have slight problem getting it to work. 我有一个很简单的问题,但我想让它正常工作有一点问题。 I have wrote a function in an external js file which has one parameter. 我已经在具有一个参数的外部js文件中编写了一个函数。

In my HTML I have onclick event where the arguement I want to supply is the value in a dropdown menu, hence me using document.getElementById to grab that value. 在我的HTML中,我有onclick事件,我要提供的论据是下拉菜单中的值,因此我使用document.getElementById来获取该值。 But when I trigger the event it is complaining. 但是当我触发事件时,它正在抱怨。

<select id="ddlOriginHubChinaSea" onclick="chkHub(document.getElementById('ddlOriginHubChinaSea').val())" >
                <option value="Cambodia">Cambodia</option>
                <option value="HK">HK</option>
                <option value="Indonesia">Indonesia></option>
                <option value="Shanghai">Shanghai</option>
                <option value="Thailand">Thailand</option>
</select>


function chkHub(param) {  
    if (param != '') {
        $('#txtChinaSeaAirHub').val(param);       
    }
    else {
        $('#txtChinaSeaAirHub').val('');
    }
}

试试看:

<select id="ddlOriginHubChinaSea" onchange="chkHub(this.value)" >
function chkHub(param) {  
    if (param.options[param.selectedIndex].value != '') {
        $('#txtChinaSeaAirHub').val(param.options[param.selectedIndex].value);       
    }
    else {
        $('#txtChinaSeaAirHub').val('');
    }
}

and onchange="chkHub(this)" onchange="chkHub(this)"

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

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