简体   繁体   中英

Send text-box id to function in JavaScript?

I tried to get the text-box's id via its event function but it seems $a doesn't work because the system understands this as text, what should I do?

<select class="btn btn-outline-secondary dropdown-toggle"
        type="button"
        class="dropdown-menu" 
        onchange="validateSelectBox(this,'skill_input4')">

<select class="btn btn-outline-secondary dropdown-toggle"
        type="button"
        class="dropdown-menu" 
        onchange="validateSelectBox1(this,'skill_input3')">
function validateSelectBox(obj, $a){
  
    var options = obj.children;
 
    var html = '';
 
    for (var i = 0; i < options.length; i++){
        if (options[i].selected){
            html += options[i].value;
        }
    }
    // this is the error, the textbox does not change data
    // when I select the combobox
    document.getElementById('$a').value = html; 
}

你不需要把它放在引号中,因为它是一个变量,你能试试这个吗

document.getElementById($a).value = html; 
  function validateSelectBox(obj, a)
{
  
    var options = obj.children;
 
    var html = '';
 
  
    for (var i = 0; i < options.length; i++){
        if (options[i].selected){
            html += options[i].value;
        }
    }
document.getElementById(a).value = html; 
}

如果skill_input3 和skill_input4 是提到的文本框的id,你应该调用document.getElementById($a).value=html

 function validateSelectBox(obj,$a)
{
  
    var options = obj.children;
 
    var html = '';
 
  
    for (var i = 0; i < options.length; i++){
        if (options[i].selected){
            html += options[i].value;
        }
    }
document.getElementById($a).value = html;//remove '$a' from quotes.
}

Remove'$a' From Quotes

You have to remove $a from quotes as it a variable

function validateSelectBox(obj,$a)
{
  
    var options = obj.children;
 
    var html = '';
 
  
    for (var i = 0; i < options.length; i++){
        if (options[i].selected){
            html += options[i].value;
        }
    }
document.getElementById($a).value = html;//remove '$a' from quotes.
}

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