如何正确转换onclick到onsubmit?

时间:2015-08-25 22:33:46

标签: javascript onclick onsubmit

我有一个用于将文件上传到我们的google驱动器的表单,我需要为某些字段创建验证。经过大量的Google搜索后,我发现您需要使用var m = new MenuFlyout { Placement = FlyoutPlacementMode.Full, MenuFlyoutPresenterStyle = (Style)this.Resources["CenteredMenuFlyoutPresenterStyle"] }; var t = new MenuFlyoutItem() { Text = "SomeTxt" }; m.Items.Add(t); m.ShowAt((FrameworkElement)sender); 代替onsubmit来验证是否有效。

然而,当我将onclick转换为onsubmit,当验证开始工作时,文件上传功能中断,当有人点击提交(“上传文件”)按钮时没有任何反应。

原始代码如下:

onclick

我将<form id="myForm" > <center>Please deposit your file using the submission form below.</center> <br> <br> <font size="3" color="red">*</font>Your Name: <input type="text" name="myName" placeholder="Your name.." required /> <font size="3" color="red">*</font>Your Email: <small><b>(to contact in case of questions, and send your file deposit receipt)</b></small> <input type="email" name="userMail" placeholder="Your email.." required /> <br> <font size="3" color="red">*</font>File Recipient Name: <input type="text" name="recName" placeholder="Your recipient's name.." required /> <font size="3" color="red">*</font>File Recipient Email: <small><b>(whom to notify of your deposit)</b></small> <input type="email" name="recEmail" placeholder="Your recipient's email.." required /> <br> School: <small><b>(if your file pertains to a specific school)</b></small> <input type="text" name="aSchool" placeholder="Pertaining school.." /> School Year: <small><b>(if your file pertains to a specific school year)</b></small> <input type="text" name="aSchoolYear" placeholder="Pertaining school year.." /> <br> Note(s): <small><b>(any special note about the file)</b></small> <br> <textarea name="sNote" rows="3" cols="40" > </textarea> <br> <br> <input type="file" name="myFile" required /> <br> <input type="submit" value="Upload File" onclick="this.value='Uploading..'; google.script.run.withSuccessHandler(fileUploaded) .uploadFiles(this.parentNode); return false;" /> </form> 转换为onclick,如此:

onsubmit

关于我做错了什么或是否有不同的方法进行验证并保留<input type="submit" value="Upload File" onsubmit="this.value='Uploading..'; google.script.run.withSuccessHandler(fileUploaded) .uploadFiles(this); return false;" /> 的任何想法?

1 个答案:

答案 0 :(得分:0)

当您更改为onsubmit时,this将成为表单本身,而不是提交按钮。

更好的方法(在我看来)是用JQuery语法实现事件,比如

$(document).on('submit', "#myForm", function(e) {
    var formObj = $(e.currentTarget);
    formObj.find("input[type='submit']").val("Uploading..");
    ....
});