简体   繁体   English

我可以在Javascript的GetElement内部调用另一个函数吗

[英]Can i call another function inside the GetElement in Javascript

I am trying to call another function inside the getElement but it is not working everything when i change my selection. 我试图在getElement内调用另一个函数,但是当我更改选择时它不能正常工作。 When i select Car, in the textbox my varxumb should populate. 当我选择Car时,在文本框中应填写我的varxumb。 Any idea... 任何想法...

document.getElementById("mycall1").insertRow(-1).innerHTML = '<td><select id = "forcx" onchange="fillgap()"><option>Select</option><option>Force</option><option>Angle</option><option>Area</option></select></td>';
function fillgap() {
    var xnumb = 20;
    var forcxlist = document.getElementById("forcx");
    if (forcxlist == "Force") {
        document.getElementById("result1").value = xnumb;
    }
}

I don't know how this "Force" value is coming to check. 我不知道这个“ Force”值将如何检查。 you can try these solutions. 您可以尝试这些解决方案。

if (forcxlist == "Force")

instead use 代替使用

var forcxlistText = forcxlist.options[forcxlist.selectedIndex].text;
if (forcxlistText == "Force")

or use value technique 或使用价值技术

<div id ="mycall1">
</div>
<div id ="result1">
</div>
<script>
    document.getElementById("mycall1").innerHTML = '<td><select id = "forcx" onchange="fillgap(this.value)"><option value="1">Select</option><option value="2">Force</option><option value="3">Angle</option><option value="4">Area</option></select></td>';

    function fillgap(value){
         var xnumb = 20;         
         if (value == "2"){
             document.getElementById("result1").innerHTML = xnumb;    
         }
    }
</script>

or use 或使用

<div id ="mycall1">
</div>
<input type="text" id="result1" value=""/>
<script>
    document.getElementById("mycall1").innerHTML = '<td><select id = "forcx"><option value="1">Select</option><option value="2">Force</option><option value="3">Angle</option><option value="4">Area</option></select></td>';

    document.getElementById("forcx").onchange = function (){
        var xnumb = 20; 
        var forcxlist = document.getElementById("forcx");
        var forcxlistValue = forcxlist.options[forcxlist.selectedIndex].value;
        if (forcxlistValue == "2"){
            document.getElementById("result1").value = xnumb;    
        }
    }
</script>

The forcxlist variable is an element object, returned by the document.getElementById method. forcxlist变量是一个元素对象,由document.getElementById方法返回。 Afterwards, you are checking if this element object is equal to "Force", which is a string (meaning the contents of your if block will never be executed). 然后,您要检查此元素对象是否等于“ Force”,它是一个字符串(表示if块的内容将永远不会执行)。 Did you mean to check if the contents of that object are equal to Force? 您是否要检查该对象的内容是否等于Force?

Instead of 代替

 if (forcxlist == "Force"){

use 采用

if (forcxlist.innerHTML == "Force"){

I hope this helps! 我希望这有帮助!

无法使用innerHTML,所以我将其更改为.value

document.getElementById("result1").value = xnumb;    

There are a couple issues here. 这里有几个问题。

First, you are expecting forcxlist to be a string, not an element, so you need to use .value to get the selected value of the dropdown. 首先,您期望forcxlist是一个字符串,而不是一个元素,因此您需要使用.value来获取下拉列表的选定值。 Second, you should do your comparison with === not == , as this ensures type equality as well, and is best practice. 其次,应该使用=== not ==进行比较,因为这也可以确保类型相等,并且是最佳实践。

I would also recommend building your select using HTML elements. 我还建议您使用HTML元素构建您的select It keeps things cleaner, is more readable, and is easier to maintain. 它使内容更清洁,更易读且易于维护。

Since you are using the same id for the select , you would have to change the selector in your fillgap handler to var forcxlist = e.target.value; 由于对select使用相同的id ,因此必须将fillgap处理程序中的选择器更改为var forcxlist = e.target.value; , this way the event will fire based on only the select that you are interacting with, regardless of how many rows you have in the table. ,这样,事件将仅基于您正在与之交互的select而触发,而不管表中有多少行。

Updated code is below, and an updated working fiddle here . 更新的代码在下面,更新的工作提琴在这里 As per your comment about adding additional rows, the fiddle has this working as well. 根据您对添加其他行的评论,小提琴也可以正常工作。

<input type="button" value="Add Row" onclick="addDropDown()">
<table id="mycall1"></table>

<script>
  function addDropDown() {
    var tbl = document.getElementById("mycall1");
    var newRow = tbl.insertRow(-1);
    var newCell = newRow.insertCell(0);
    newCell.appendChild(createDropDown("forcx", fillgap));
  }

  function createDropDown(id, onchange) {
    var dd = document.createElement('select');
    dd.id = id;
    dd.onchange = onchange;
    createOption("Select", dd);
    createOption("Force", dd);
    createOption("Angle", dd);
    createOption("Area", dd);
    return dd;
  }

  function createOption(text, dropdown) {
    var opt = document.createElement("option");
    opt.text = text;
    dropdown.add(opt);
  }

  function fillgap() {
    var xnumb = 20;
    var forcxlist = e.target.value;

    if (forcxlist === "Force") {
        document.getElementById("result1").value = xnumb;
    }
  }
</script>

<input type="text" id="result1">

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

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