开发者

jQuery/JavaScript - Displaying value from text-input in <span>-placeholders

I FORMULATED MY SELF VERY BADLY!

I will start over :) I appreciate your good answers, and if you can, try answering this: ( I will try to be more specific this time)

What I want is, that a <form>element onsubmit, onclick of a button or whatever takes the value of an <input type="text" value="Default value"> and inserts it in a couple of <span>elements, I like to call "placeholders". This sample might explain it a little better:

<head>
<script type="text/javascript">
$(document).ready(function() 
{
$("input[type=button]").click(function //click the button
{ do_the_magic_thing() //make the value from id="txt_inp" appear in class="placeholder"
}); 
});
</script>
</head>

<body>
<form method="POST" action="" id="theForm"> //could also be get, I don't care
<input type="text" id="txt_inp" value="Default Value" onfocus="if (this.value == Default Value) this.value=''"> //this SHOULD make the Default Value dissapear on focus
<input type="button"> //could also be a submit

<span class="placeholder"></span> //$("#txt_inp").value; goes here
<span class="placeholder"></span> //$("#txt_inp").value; goes here
</body>

Now, is it really as simple as this?:

var do_the_magic_thing = function() {
$(".placeholder").html = $("#txt_inp").value;
};

I'm going to bed now - it's late in Denmark :) I will answer your comments tomorrow :)

OLD POST:

I am very new to this jQuery thing, but I do understand 开发者_开发知识库the basics and all. Let's simplify and say I have a form which looks like this:

<form method="POST" action="">
<input type="text" value="Default value">
<input type="submit" value="Click me">
<input type="hidden"> //hidden is for being able to submit by hitting enter
</form>

I've tried with $.post, but I just cannot get it right; it's not working for me.

Now, I would like to cancel the submit (can't that be done by just adding a return false; in the function returning the value, if a such is present?), but this is not crucial.

I think I typed in something like

$.post("test.php", function(data) {

alert("This is the data submitted (and cancelled):" + data);

}); //I have also tried without the "test.php", that's not it

Can you tell me, what I'm doing wrong please? :)

NOTE

It is not necessary, that the submit is cancelled, but I would like that

Nor is it necessary, that POST is the method used, but once again, this is what I prefer


Change the id of your form to "myform" or whatever and the name of your text input to "myinput", and try something like this...

$(document).ready(function() {
    $('#myform').submit(submitMyForm);
})

function submitMyForm(e) {
    var data = new Object();
    data.whatever = $('#myinput').val();

    var post = new Object();
    //here I use a jquery json extension...you can use anything you like
    post.data = $.toJSON(data);

    $.post("test.php", post, function(returnval) {
        alert(returnval);
    }, "text");

    //this is to stop the normal form submit action
    return false;
}

Then in your test.php you can access it by calling $_POST['data'] (we specified this when we created the property of the "post" object called "data" like this: post.data = 'whatever'.


To answer the revised version of your question, yes, it really is that simple, although the correct syntax for your "do the magic thing" function is the following:

var do_the_magic_thing = function() {
    $('.placeholder').html($('#txt_inp').val());
};

P.S. Don't worry too much about not expressing yourself, your English is much better than my Danish.


I think what you want to do is something like this:

<fieldset id="myData">
    <legend>My Data</legend>
</fieldset>

<form id="myForm" method="POST" action="">
    <input type="text" value="Default value">
    <input type="submit" value="Click me">
    <input type="hidden"> //hidden is for being able to submit by hitting enter
</form>

$(function() {
   $('#myForm').submit(function() {
      //do whatever you want here.
      //this will take place after the form is submitted, but before your ajax request
      $('input[type=text]').each(function() {
          $('#myData').append('<div class="placeholder">' + $(this).val() + '</div>');
      });

      //serialize your form data
      var toSubmit = $('input[type=text]').serialize();

      //do ajax here
      $.post('test.php', toSubmit, function(data) {
        alert('Your AJAX POST request returned: ' + data);
      }, 'text');

      //this will prevent the form from submitting normally
      return false;
   });
});

Here's a demo of this in action: http://jsfiddle.net/SA3XY/

Also see my comment on your question.


Well for the form submit you need to add the following to the form to cancel the default submit event:

<form onsubmit="return functioncall();">

Then when you return false from the function it will cancel the default form action.

EDIT: If you would like to see all the data that is to be submitted you can serialize the form using jquery serialize() method or serializeArray() method.


If you're trying to accomplish validation, there's a much easier way, just use a validation plugin like this one: http://docs.jquery.com/Plugins/Validation

Makes it much easier and takes the headache out of developing your own code. Jquery makes it easy to develop powerful javascript applications...but sometimes it's just easier to use stuff that's already been written and debugged (for the most part at least).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜