return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - HTML form with custom function call - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML form with custom function call - Stack Overflow

programmeradmin2浏览0评论

I am developing Universal windows 8.1 app and I have a page for creating new contact. In purpose to validate input fields I put them in form. My form action not supposed to go to service it just need to validate fields and call custom function.

HTML:

<form name="myform" onSubmit="JavaScript:OnSubmitForm()">
     <div>
         <label>
             First name<br />
             <input id="contactFirstName" class="win-textbox" type="text" name="firstName" required />
         </label>
     </div>
     <div>
         <label>
             Last name<br />
             <input id="contactLastName" class="win-textbox" type="text" name="lastName" />
         </label>
     </div>
     <input type="submit" value="Submit"/>
     <input type="button" value="Cancel"/>
</form>

JavaScript:

document.addEventListener('DOMContentLoaded', function() {
    function OnSubmitForm()
    {
        alert('click');
    }
});

My alert is never calls in plain HTML project neither in WinJS app. I also tried with:

  <form name="myform" onsubmit="return OnSubmitForm();">

and the same.

Does it a good approach at all, to use form just for input fields validation or there is better way, and why this does not work ?

I am developing Universal windows 8.1 app and I have a page for creating new contact. In purpose to validate input fields I put them in form. My form action not supposed to go to service it just need to validate fields and call custom function.

HTML:

<form name="myform" onSubmit="JavaScript:OnSubmitForm()">
     <div>
         <label>
             First name<br />
             <input id="contactFirstName" class="win-textbox" type="text" name="firstName" required />
         </label>
     </div>
     <div>
         <label>
             Last name<br />
             <input id="contactLastName" class="win-textbox" type="text" name="lastName" />
         </label>
     </div>
     <input type="submit" value="Submit"/>
     <input type="button" value="Cancel"/>
</form>

JavaScript:

document.addEventListener('DOMContentLoaded', function() {
    function OnSubmitForm()
    {
        alert('click');
    }
});

My alert is never calls in plain HTML project neither in WinJS app. I also tried with:

  <form name="myform" onsubmit="return OnSubmitForm();">

and the same.

Does it a good approach at all, to use form just for input fields validation or there is better way, and why this does not work ?

Share Improve this question asked Apr 21, 2016 at 12:20 MilosMilos 1,8784 gold badges28 silver badges56 bronze badges 4
  • Do you see error log in Browser's console of undefined function or something ? – Alok Patel Commented Apr 21, 2016 at 12:23
  • I see that error "uncaught reference" logged in console on button click, but error just blink i cannot catch it. Also input filed are empty after submit like everything is ok. – Milos Commented Apr 21, 2016 at 12:32
  • return false in the function OnSubmitForm() so that your page doesn't redirect and you can see the error. – Alok Patel Commented Apr 21, 2016 at 12:34
  • Even with "return false" I could not catch the error, error just blinked. – Milos Commented Apr 21, 2016 at 13:01
Add a ment  | 

2 Answers 2

Reset to default 5

Inline event-binding expects functions to be under global-scope and that is one of the reason one should not use it!

In your example, OnSubmitForm is under the local scope of DOMContentLoaded handler. Best approach would be to use addEventListener and with the current code, place OnSubmitForm out of DOMContentLoaded.

document.getElementById('myform').addEventListener('submit', function(e) {
  e.preventDefault(); //to prevent form submission
  alert('click');
});
<form name="myform" id='myform'>
  <div>
    <label>
      First name
      <br />
      <input id="contactFirstName" class="win-textbox" type="text" name="firstName" required />
    </label>
  </div>
  <div>
    <label>
      Last name
      <br />
      <input id="contactLastName" class="win-textbox" type="text" name="lastName" />
    </label>
  </div>
  <input type="submit" value="Submit" />
  <input type="button" value="Cancel" />
</form>

With current approach:

function OnSubmitForm() {
  alert('click');
}
<form name="myform" onSubmit="JavaScript:OnSubmitForm()">
  <div>
    <label>
      First name
      <br />
      <input id="contactFirstName" class="win-textbox" type="text" name="firstName" required />
    </label>
  </div>
  <div>
    <label>
      Last name
      <br />
      <input id="contactLastName" class="win-textbox" type="text" name="lastName" />
    </label>
  </div>
  <input type="submit" value="Submit" />
  <input type="button" value="Cancel" />
</form>

Have you tried parsley.js?

I created this fiddle to show you a quick example.

<form name="myform">
 <div>
     <label>
         First name<br />
         <input id="contactFirstName" data-parsley-required-message="First name required" class="win-textbox" type="text" name="firstName" required data-parsley-trigger="change focusout" data-parsley-pattern="/^[a-zA-Z]*$/"/>
     </label>
 </div>
 <div>
     <label>
         Last name<br />
         <input id="contactLastName" class="win-textbox" type="text" name="lastName" />
     </label>
 </div>
 <input type="submit" value="Submit"/>
 <input type="button" value="Cancel"/>

<script src="http://parsleyjs/dist/parsley.js"></script>
<script>
    window.ParsleyConfig = {
        errorsWrapper: '<div></div>',
        errorTemplate: '<div class="alert alert-danger parsley" role="alert"></div>',
        errorClass: 'has-error',
        successClass: 'has-success'
    };
</script>
发布评论

评论列表(0)

  1. 暂无评论