onchange this.form.submit()不适用于Web表单 [英] onchange this.form.submit() not working for web form

查看:105
本文介绍了onchange this.form.submit()不适用于Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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>

我看到错误
未捕获的TypeError:无法调用方法'提交' nullonchange

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

我也尝试过onchange =javascript:document.orderbyfrm.submit但这也无效。

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

推荐答案

可能你有名为表格的元素或JS对象提交某处,与真实形式冲突。

Probably you have element or JS object called form or submit somewhere, conflicting with the real form.

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

Most safe way is using document.getElementById:

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

以及JavaScript:

And the JavaScript:

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

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

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

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.

这篇关于onchange this.form.submit()不适用于Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆