Firefox没有将值从输入文本字段传递到javascript函数

时间:2021-08-07 09:51:05

I have a problem with firefox passing information into a javascript function. The following code works ok on Chrome, Safari and Opera. It has some issues in IE9 but overall does what it is supposed to.

我有一个问题,Firefox将信息传递到javascript函数。以下代码适用于Chrome,Safari和Opera。它在IE9中有一些问题,但总体上做了它应该做的事情。

Firefox however simply ignores the onchange and onclick with the console reporting "query is not defined". If there is a way around this or a different approach I'd love to know.

然而,Firefox只是忽略了onchange和onclick,控制台报告“查询未定义”。如果有办法绕过这个或不同的方法,我很想知道。

<div class="viewstory_controls_content">
                Search
                <input type="text" id="query" onchange="javascript:submitForm('../scripts/php/search.php', 'search_results',query,'main')" name="query"></input>
                <button class="viewstory_search" onclick="javascript:submitForm('../scripts/php/search.php', 'search_results', query, 'main')">
                    <div class="viewstory_search_content">
                        Search
                    </div>
                </button>
            </div>

submit query:

提交查询:

function submitForm(url, target, term, type) {
    // native XMLHttpRequest object
    term = term.value;
    url = url + "?term=" + term + "&type=" + type;
    alert("url:" + url + "\ntarget:" + target + "\nterm:" + term);
    document.getElementById(target).innerHTML = 'sending...';
    if (window.XMLHttpRequest) {

        req = new XMLHttpRequest();
        req.onreadystatechange = function() {
            jahDone(target);
        };
        req.open("GET", url, true);
        req.send(null);
    }
    else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {
                submitFormDone(target);
            };
            req.open("GET", url, true);
            req.send();
        }
    }
}​

2 个解决方案

#1


1  

I think this is because the browsers said to support your script actually look for a field whose name attribute is equal to query (please note I'm not 100% sure on this). As far as I know, this never worked in Firefox.
Simply replace query in the event handlers with document.getElementById('query'). In the onchange handler of the input, you can use this instead of query as well.
Also, don't use javascript: in inline event handlers. It's obsolete.

我认为这是因为浏览器说支持你的脚本实际上寻找一个名称属性等于查询的字段(请注意我对此不是100%肯定)。据我所知,这在Firefox中从未奏效。只需使用document.getElementById('query')替换事件处理程序中的查询。在输入的onchange处理程序中,您也可以使用它而不是查询。另外,不要在内联事件处理程序中使用javascript:它已经过时了。

#2


0  

term is the third parameter , you are passing query there in function call like a variable. It is not defined anywhere . Pass the value of the field query

term是第三个参数,你在函数调用中传递查询就像一个变量。它没有在任何地方定义。传递字段查询的值

#1


1  

I think this is because the browsers said to support your script actually look for a field whose name attribute is equal to query (please note I'm not 100% sure on this). As far as I know, this never worked in Firefox.
Simply replace query in the event handlers with document.getElementById('query'). In the onchange handler of the input, you can use this instead of query as well.
Also, don't use javascript: in inline event handlers. It's obsolete.

我认为这是因为浏览器说支持你的脚本实际上寻找一个名称属性等于查询的字段(请注意我对此不是100%肯定)。据我所知,这在Firefox中从未奏效。只需使用document.getElementById('query')替换事件处理程序中的查询。在输入的onchange处理程序中,您也可以使用它而不是查询。另外,不要在内联事件处理程序中使用javascript:它已经过时了。

#2


0  

term is the third parameter , you are passing query there in function call like a variable. It is not defined anywhere . Pass the value of the field query

term是第三个参数,你在函数调用中传递查询就像一个变量。它没有在任何地方定义。传递字段查询的值