繁体   English   中英

发送文本框 ID 以在 JavaScript 中运行?

[英]Send text-box id to function in JavaScript?

我试图通过其事件函数获取文本框的 id,但似乎$a不起作用,因为系统将其理解为文本,我该怎么办?

<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.
}

从引号中删除'$a'

您必须从引号中删除 $a 作为变量

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.
}

暂无
暂无

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

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