简体   繁体   中英

append required input into existing form

I've got a small-big problem with ajax. Let's describe the situation:

  1. I've got a form with submit=javascript:function()

  2. function will call AJAX with some values, and on success I want to append some content with 'required' input to existing form.

I was trying many things, most from: How to set HTML5 required attribute in Javascript? , but still cannot reach it.

example code:

<form id="myFormID" action="javascript:mycustomsubmit()">
    <input type="text" id="add" style="margin:2px;" required>
    <input type="submit" name="add" value="Add" class="btn btn-primary">
    <textarea rows="5"  id="custom_add"></textarea>

(...) on ajax success clear form values and insert new required input:

$("#add").val('');
$("#add").after('<input name="anotherinput" type="text" required>');

so after this my html code looks like this:

<form id="myFormID" action="javascript:mycustomsubmit()">
        <input type="text" id="add" style="margin:2px;" required>
        <input name="anotherinput" type="text" required="">
        <input type="submit" name="add" value="Add" class="btn btn-primary">
        <textarea rows="5"  id="custom_add"></textarea>
</form>

And in fact it is (with this difference, that new input has required=""), but this new input is not required at all - I can send form even if this input is empty. I was trying to do it by append required, required="required", required=true, required="true", by append just input and then jQuery .prop or/and .attr, by JS examples from link - but it is still not working.

2nd question: After ajax append content and clear values I've got red border around required input field - is there any simple way to remove it (but to show this border and info if user will try to send form with this input empty) in FF,Chrome,IE ?

First post here...

Thanks in advance for any advices!

edit: what is interesting: when I've submitted my form few times (so I've got few input fields) and I executed $("input").attr('required',true).prop('required', false); then obviously form haven't got any required inputs. However when I've executed it with prop "true" then only original input is really required, all added by append still can be empty...

Try this,

$("input").attr('required',true);

or

$("#name_add").attr('required',true);

This is a question consisting of multiple questions. So I'll try to identify and answer them separately.

  1. How to append a new input field after your input field with ID "add" on submitting the form?

Try this instead (your selector was wrong):

$("#add").val('');
$("#add").after('<input name="anotherinput" type="text" required>');
  1. How do I get rid of the red border?

I suggest that you use jQuery to handle the form submit (not tested):

    $('#myFormID').submit(function(e) {
    // Checking if all required fields are filled out.
    if (!e.target.checkValidity()) {
        // TODO: Not all required fields are filled out. Do something e.g. adding your new input field?

        // Preventing form submit to continue. I think this should prevent the red border. Not tested though...
        e.preventDefault();
    }
    else {
        // Everything is OK. Do whatever is needed.
    }
});

I'm not sure if I got your questions, but I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM