Javascript onchange this.form.submit() 不适用于网络表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12275810/
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-08-23 07:35:15  来源:igfitidea点击:

onchange this.form.submit() not working for web form

javascriptsubmitform-submitonchange

提问by Max Hodges

been working on this way too long...but can't seem to identify the problem. Already read dozens of articles on stackoverflow and elsewhere.

以这种方式工作太久了……但似乎无法确定问题所在。已经阅读了数十篇关于 stackoverflow 和其他地方的文章。

when I click and change the value, it doesn't auto-submit:

当我单击并更改值时,它不会自动提交:

   <form id="orderbyfrm" name="orderbyfrm" action="http://staging.whiterabbitexpress.com/" method="post" class="orderbyfrm">
            <input name="s" value="<?php echo $wre_search_txt?>" type="hidden">
            <label for="orderby" class="sortByLabel">Sort by&nbsp;</label>
            <select class="sortByDropdown" name="orderby" id="orderby" onchange="this.form.submit();">
                <option value="Relevance">Relevance</option>
                <option value="likes" selected="selected">Likes</option>
            <option value="comments" selected="comments">Comments</option>
            </select>
</form>

in Chrome inspector I see an error "Uncaught TypeError: Cannot call method 'submit' of null" onchange

在 Chrome 检查器中,我看到一个错误“Uncaught TypeError:Cannot call method 'submit' of null” onchange

I also tried onchange="javascript:document.orderbyfrm.submit" but that didn't work either.

我也试过 onchange="javascript:document.orderbyfrm.submit" 但这也不起作用。

回答by Shadow Wizard is Ear For You

Probably you have element or JS object called formor submitsomewhere, conflicting with the real form.

也许你有元素或JS对象调用formsubmit某处,与真正形成冲突。

Most safe way is using document.getElementById:

最安全的方法是使用 document.getElementById:

<select onchange="SubmitForm('orderbyfrm');">

And the JavaScript:

和 JavaScript:

function SubmitForm(formId) {
    var oForm = document.getElementById(formId);
    if (oForm) {
        oForm.submit(); 
    }
    else {
        alert("DEBUG - could not find element " + formId);
    }
}

Further debugging with good old alert.. instead of the alert("DEBUG ...have this:

使用良好的旧警报进行进一步调试.. 而不是alert("DEBUG ...有这个:

var sDebugInfo = "found " + document.forms.length + " forms: \n";
for (var i = 0; i < document.forms.length; i++) {
    var curForm = document.forms[i];
    sDebugInfo += "name: " + curForm.name + ", id: " + curForm.id;
    sDebugInfo += "\n";
}
alert(sDebugInfo);

Depending on what you get, debug should continue.

根据您得到的内容,调试应该继续。

回答by Max Hodges

sorry guys! I found the problem. I had a broken div around this form

对不起大家!我发现了问题。我在这个表单周围有一个坏掉的 div

<div id="orderby" class="orderby
<form id="xxx" name="xxx" action="#" method="post" class="orderbyfrm">

fixed:

固定的:

Really appreciate your help everyone!

真的很感谢大家的帮助!