简体   繁体   中英

How To output form input value using appendTo with jquery

Alright Before Getting Started Here is the idea . i'm making a reminder app it's just a simple app . i'm getting the value from the input field and making a checkbox with the input value next to it . now i'm using appendTo method but it's not printing the value . if you guys can help it would be awesome thanks!

在此处输入图片说明

MY HTML

 <form id="form">
   <input class="form-control topSpaceFromRoof" type="text"id="getReminder"/>
   <p class="text-center">
      <input type="submit" value="submit" class="btn btn-default">
   </p>
 </form>

 <!-- here is the div in which i want to append check boxes -->
 <div class="checkbox">

 </div>

MY JAVASCRIPT

 (function(){

    var input = $('#getReminder');
    $this     = $(this);

    $( "form" ).submit(function() {
     if(input.val() == ""){
       input.addClass('warning').attr('placeholder','Please set the reminder').addClass('warning-red');
       return false;
     }
    else
    {
        input.removeClass('warning');
        $('<label>
            <input type="checkbox" id="checkboxSuccess" value="option1">
            '+ input.val() +'
           </label>').appendTo('.checkbox');
        return true;
    }

});

})();

In JavaScript, a new line within a string denotes the start of a new statement. If you want your string to cover multiple lines, you need to escape the carriage return:

 $('<label>\
    <input type="checkbox" id="checkboxSuccess" value="option1">\
    '+ input.val() +'\
    </label>').appendTo('.checkbox');

Or, concatenate each line:

 $('<label>' +
    '<input type="checkbox" id="checkboxSuccess" value="option1">' +
    input.val() +
    '</label>').appendTo('.checkbox');
else { 
   input.removeClass('warning');
    $('.checkbox').append('<label><input type="checkbox" id="checkboxSuccess" value="option1"> ' + input.val() + '</label>');
    return true;
 }

I'm not sure .appendTo() is what you want to use here, I would select the element with $(selector) and then use the .append() method to add the next item.

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