Home>

I am making the process before sending the comment form with jQuery.
When I click the send button, I want to send it after putting @hanako in<textarea>.

Problems i am experiencing

In the source code below, even if you clickafter entering<textarea>, @hanakop is not entered.// Problem locationdoes not work properly.
(For some reason, @hanako is entered if it is not input immediately after reading.)

I asked you to tell me how to write @hanakop.
Thank you.

Applicable source code
&lt;div&gt;
    &lt;form name = "new-post" method = "post" action = "http://example.com/thread/testpost/"&gt;
        &lt;div&gt;
            &lt;div&gt;
                &lt;p&gt;Reply to:&lt;span&gt;@ hanako&lt;/span&gt;&lt;/p&gt;
                &lt;textarea name = "reply_content" cols = "60" rows = "12" tabindex = "100" placeholder = "comment"&gt;&lt;/textarea&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;button type = "submit" tabindex = "104" name = "reply_submit"&gt;Send&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;input name = "reply_to" value = "1" type = "hidden"&gt;
    &lt;/form&gt;
&lt;/div&gt;
$('body'). on ("click", '# reply_submit', function () {
  // If the value of #reply_to is other than 0, send after the following processing
  var targetid = $('# reply_to'). val ();
  if (targetid! == 0) {
    var replyuser = $('# replyuser'). text ();
    $("# reply_content"). prepend (replyuser);// Problem
    $("# new-post"). submit ();
  // Send normally when value of #reply_to is 0
  } else {
    $("# new-post"). submit ();
  }
});
Resolve

Thanks for your response.
UsepreventDefault ()and modifyval ()instead of inserting it withprepend (). The following source code by two methods.

$('body'). on ("click", '# reply_submit', function (e) {
  // Stop processing once
  e.preventDefault ();
  // If targetid is other than 0, modify "val ()" and then "submit ()".
  var targetid = $('# reply_to'). val ();
  if (targetid! = 0) {
    var replyuser = $('# replyuser'). text ();
    var replytext = $("# reply_content"). val ();
    $("# reply_content"). val (replyuser + $("# reply_content"). val ());
    $("# new-post"). submit ();
  // When targetid is 0, "submit ()" is done normally.
  } else {
    $("# new-post"). submit ();
  }
});


Thank you very much x_x and mts10806.

  • Answer # 1

    Submit sends information at the time the button is pressed, so
    If the button type is submit, it may actually be added later or submit () is meaningless.
    In other words, if you send it without permission, it can be said that it is synonymous, so let's set the type to button.
    Or stop SUBMIT at the beginning of the click event with event.preventDefault.

  • Answer # 2

    $("# reply_content"). val (replyuser + $("# reply_content"). val ());

    I think that it is better to send @hanako separately because there is a problem that it will be granted again if it is not sent, or it will not work when the script is disabled.


    Why did you soon be able to choose jQuery as the language?