简体   繁体   English

jQuery根据表单输入的值更改段落span标签的文本

[英]jQuery change text of paragraph span tag according to the value of form input

I have a class of 'friends-name' on the span of the paragraph that should take the value of 'input[name="friends-first-name"]. 我在该段的跨度上有一个“ friends-name”类,该类应采用“ input [name =“ friends-first-name”]的值。

I have a class of 'your-first-name' on the span of the paragraph that should take the value of 'input[name='your-first-name']. 我在该段的跨度上有一个“您的名字”类,该类的值应为“ input [name ='您的名字”]。

My code is as follows: 我的代码如下:

           <div class="form-message">
                <p>Hey <span class="friends-name">[Friend's First Name],</span></p>
                <p>I wanted to reach out b/c I bumped into a solid company. They do fulfillment - shipping - for rising stars</p>
                <p>I'll let you guys take it from there!</p>
                <p class="your-first-name">[Your First Name]</p>
            </div>


           <form id="referral-form" method="POST">

                    <div class="form-left">
                        <div class="form-fieldset form-fieldset_type-personal">
                            <h3 class="form-fieldsetHeading">Your Info</h2>
                            <input class="form-field" data-bind="value: firstName" type="text" name="your-first-name" placeholder="First Name">
                            <input class="form-field" data-bind="value: lastName" type="text" name="your-last-name" placeholder="Last Name">
                            <input class="form-field" data-bind="value: email" type="email" name="your-email" placeholder="Email">
                        </div>
                    </div>

                    <div class="form-right">
                        <div class="form-fieldset form-fieldset_type-friends">
                            <h3 class="form-fieldsetHeading">Friend's Info</h2>
                            <div data-bind="foreach: friends">
                                <div class="form-friend">
                                    <input class="form-field" data-bind="value: firstName" name="friends-first-name" type="text" placeholder="First Name">
                                    <input class="form-field" data-bind="value: lastName" name="friends-last-name" type="text" placeholder="Last Name">
                                    <input class="form-field" data-bind="value: company" name="friends-company" type="text" placeholder="Company">
                                    <input class="form-field" data-bind="value: email" name="friends-email" type="email" placeholder="Email">
                                </div>
                            </div>

                        </div>
                    </div>
            </form>

Any way to do this using jQuery? 有什么办法使用jQuery吗?

If you want to update it on each user input, Use keyup event. 如果要在每个用户输入上更新它,请使用keyup事件。

The keyup event is sent to an element when the user releases a key on the keyboard. 当用户释放键盘上的键时, keyup事件发送到元素。 It can be attached to any element, but the event is only sent to the element that has the focus. 它可以附加到任何元素,但是事件仅发送到具有焦点的元素。

Try this: 尝试这个:

 $('[name="your-first-name"]').on('keyup', function() { $('.your-first-name').text(this.value); }); $('[name="friends-first-name"]').on('keyup', function() { $('.friends-name').text(this.value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="form-message"> <p>Hey <span class="friends-name">[Friend's First Name],</span> </p> <p>I wanted to reach out b/c I bumped into a solid company. They do fulfillment - shipping - for rising stars</p> <p>I'll let you guys take it from there!</p> <p class="your-first-name">[Your First Name]</p> </div> <form id="referral-form" method="POST"> <div class="form-left"> <div class="form-fieldset form-fieldset_type-personal"> <h3 class="form-fieldsetHeading">Your Info</h3> <input class="form-field" data-bind="value: firstName" type="text" name="your-first-name" placeholder="First Name"> <input class="form-field" data-bind="value: lastName" type="text" name="your-last-name" placeholder="Last Name"> <input class="form-field" data-bind="value: email" type="email" name="your-email" placeholder="Email"> </div> </div> <div class="form-right"> <div class="form-fieldset form-fieldset_type-friends"> <h3 class="form-fieldsetHeading">Friend's Info</h3> <div data-bind="foreach: friends"> <div class="form-friend"> <input class="form-field" data-bind="value: firstName" name="friends-first-name" type="text" placeholder="First Name"> <input class="form-field" data-bind="value: lastName" name="friends-last-name" type="text" placeholder="Last Name"> <input class="form-field" data-bind="value: company" name="friends-company" type="text" placeholder="Company"> <input class="form-field" data-bind="value: email" name="friends-email" type="email" placeholder="Email"> </div> </div> </div> </div> </form> 

Fiddle here 在这里摆弄

如果要输入文本。

$("span.friends-name").text($("input[name='your-first-name']").val());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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