English 中文(简体)
j Query/Java-forming Value from text-input in <span>
原标题:jQuery/JavaScript - Displaying value from text-input in <span>-placeholders

I FORMULATED MY SELF VERY BADLY!

我将首先谈谈: 我赞赏你的好回答,如果你能够,我会努力回答这一点:(此时我将更具体地谈一下)。

我想的是:<form>element onsubmit, onclick of a hton or what take the Value of a <input category=“text” Value=“Default Value”>,并插入一 couple<span>elements,我叫“持有人”。 这一样本可能更能说明:

<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>

现在,这是否真的简单:

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

我现在要问一下——在丹麦晚了: 我明天将回答你的意见:

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>

我用<代码>$.post进行了尝试,但我不能这样做;我不为我工作。

现在,我要取消提交文件(如果存在的话,仅添加“<代码>return不实;在返还价值的职能中这样做),但这并非关键。

我认为,我喜欢这样作。

$.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

你们能够告诉我,我是谁错了?

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

最佳回答

回答你提出的问题的修订本,是,这确实是简单,尽管你“做魔术”功能的正确分类是:

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

P.S. Don t wor对不表示惊讶太大,你的英语比我的丹麦好得多。

问题回答

将贵表格的复制件改为“制作”,或您案文对“投影”的任何内容和名称,并尝试类似的东西......

$(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;
}

然后,在你考验中。 网址:www.un.org

我认为,你们想要做的是:

<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;
   });
});

http://jsfiddle.net

另见我对你问题的评论。

提交表格时,请在表格中添加以下内容,以取消提交违约事件的提交。

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

然后,当你放弃职务时,就会取消违约的形式行动。

EDIT:如果你想看到将要提交的所有数据,你可以采用jquery serialize(>”方法或serializeArray(<>)。

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

使其更容易,并带头制定自己的法典。 Jquery使开发强有力的javascript 申请变得容易......但有时,使用已经写成和被打脚的 st子(至少大部分是这样)。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签