简体   繁体   English

如何将上一个表单元素的值传递给“ onchange” javascript函数?

[英]How do I pass the value of the previous form element into an “onchange” javascript function?

I want to make some UI improvements to a page I am developing. 我想对正在开发的页面进行一些UI改进。 Specifically, I need to add another drop down menu to allow the user to filter results. 具体来说,我需要添加另一个下拉菜单,以允许用户过滤结果。

This is my current code: HTML file: 这是我当前的代码:HTML文件:

<select name="test_id" onchange="showGrid(this.name, this.value, 'gettestgrid')">
<option selected>Select a test--></option>
<option value=1>Test 1</option>
<option value=2>Test 2</option>
<option value=3>Test 3</option>
</select>

This is pseudo code for what I want to happen: 这是我想发生的伪代码:

<select name="test_id">
<option selected>Select a test--></option>
<option value=1>Test 1</option>
<option value=2>Test 2</option>
<option value=3>Test 3</option>
</select>
<select name="statistics" onchange="showGrid(PREVIOUS.name, PREVIOUS.VALUE, THIS.value)">
<option selected>Select a data display --></option>
<option value='gettestgrid'>Show averages by student</option>
<option value='gethomeroomgrid'>Show averages by homeroom</option>
<option value='getschoolgrid'>Show averages by school</option>
</select>

How do I access the previous field's name and value? 如何访问上一个字段的名称和值? Any help much appreciated, thx! 任何帮助,不胜感激,谢谢!

Also, JS function for reference: 另外,JS函数可供参考:

function showGrid(name, value, phpfile)
{ 
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url=phpfile+".php"; 
url=url+"?"+name+"="+value;
url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

First of all provide these select boxes with unique ids and then pass the id of the previous select box to the function and in the function get the DOM element using document.getElementById and then retrieve the name and value of that element. 首先,为这些选择框提供唯一的ID,然后将前一个选择框的ID传递给函数,然后在函数中使用document.getElementById获取DOM元素,然后检索该元素的名称和值。

<select name="test_id" id="test_id">


<select name="statistics" onchange="showGrid('test_id', THIS.value)">

function showGrid(id, phpfile)
{ 
var previousElem = document.getElementById ( id );

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url=phpfile+".php"; 
url=url+"?"+previousElem.name+"="+previousElem.value;
url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

If all your selects are siblings (children of one parent), use previousSibling to detect prev select. 如果您所有的选择都是兄弟姐妹(一个父母的孩子),请使用previousSibling检测上一个选择。

Pseudocode: 伪代码:

<select name='prev'>....</select>
<a>other tag</a>
<select name='x' onchange='handle(this);'>...</select>

function handle(select) {
    // find previous
    var prev = select.previousSibling;
    while (prev && prev.nodeName.toLowerCase() === 'select') {
        prev = prev.previousSibling;
    }
    if (prev && prev.nodeName.toLowerCase() === 'select') {
         // found
    } else {
         // not found
    }

}

I just got this page on a google for 'javascript "previous form element"'. 我刚刚在google上用“ javascript“ previous form element””获得了此页面。 As I just wanted a generic solution I tried rolling my own. 当我只想要一个通用解决方案时,我尝试了自己的滚动。 It seems to work fine. 似乎工作正常。 There are probably better ways but this does the job. 可能有更好的方法,但这可以完成工作。

function previous_form_element(form,element){
    for (i=1;i<form.elements.length;i++){
        if (form.elements[i] == element){
            return form.elements[i-1]; 
        }
    }
    return undefined;
}

... 
button.onclick = function (){
    alert(previous_form_element(this.form,this).value);
}

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

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