javascript 如何在onChange中传递两个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13135124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:54:03  来源:igfitidea点击:

How to pass two values in onChange

javascriptdom

提问by CalvinLChua

So, I have a 2 select tags and during the onChange event of the second select tag; I want to pass the values of the 2 select tags

所以,我有 2 个选择标签,并且在第二个选择标签的 onChange 事件期间;我想传递 2 个选择标签的值

    <td>District:
    <div>
    <select name="district" id="district" onchange="getZone(this.value)">
    <option value="ALL">ALL</option>
    <?php
     include_once 'newcompute.php';
     $computation = new newcompute();
     echo $computation->getDistrict();
    ?>
    </select>
    </div>
    </td>
    <td>Barangay:&nbsp;
<div id="barangay" name="barangay">
    <select onchange="loadReport(barangay.value,district.value)">
<option>Select a Barangay</option>
<option value="ALL">ALL</option>
</select>
    </div>
</td>
    </tr>

So, I tried doing the onchange="loadReport(barangay.value,district.value)" but I only get a UNDEFINED value for district.value

因此,我尝试执行 onchange="loadReport(barangay.value,district.value)" 但我只得到了 District.value 的 UNDEFINED 值

How do I get loadReport to pass the current selected value of select tag district?

如何让 loadReport 传递选择标签区的当前选定值?

回答by Shashwat

You can retrieve the other tag in the function itself. No need to pass it from here.

您可以在函数本身中检索另一个标签。无需从这里传递。

function loadReport(barangayValue)
{
    var district = document.getElementById('district');
    var districtValue = district.value;
    //Your stuff
}

回答by Sibu

Try this way

试试这个方法

<select onchange="loadReport(this.value,document.getElementById('district').value)">
<option>Select a Barangay</option>
<option value="ALL">ALL</option>
</select>