简体   繁体   中英

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.

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. 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)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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