[英]On button click assign div inner html to input field value
What I want is when someone click on button, set the phone number to input value. 我要的是当有人单击按钮时,将电话号码设置为输入值。 The block is a loop in which different email and phone number appears.
该块是一个循环,其中出现了不同的电子邮件和电话号码。 if some one click in a specific block get the phone number from that block only.
如果在特定区域中单击某个按钮,则只能从该区域获得电话号码。 Thanks.
谢谢。
<div class="block">
<div class="phone"><span>+1234567</span></div>
<div class="email">
<span>email@email.com</span>
<button>CLick</button>
</div>
</div>
<div class="block">
<div class="phone"><span>+7696686</span></div>
<div class="email">
<span>another@email.com</span>
<button>CLick</button>
</div>
</div>
<div class="block">
<div class="phone"><span>+897979877</span></div>
<div class="email">
<span>email2@email.com</span>
<button>CLick</button>
</div>
</div>
<input type="text" value="" id="phone">
Here is my code: 这是我的代码:
jQuery(document).ready(function() {
jQuery(".block button").click(function() {
var contents = jQuery(this).find(".phone span").text();
jQuery("#phone").val(contents);
});
});
You tagged jQuery so I'm assuming it's ok for you to receive an answer using jQuery. 您标记了jQuery,所以我认为您可以使用jQuery接收答案。 Try something similar to:
尝试类似的方法:
$('button').click(function(){
var phoneNumber = $(this).closest('.phone').find('span').text();
});
Try this- 尝试这个-
jQuery(document).ready(function($) { $(".block").each(function() { $(this).find('button').click(function(){ var contents = $(this).parents('.block').find('.phone span').text(); $("#phone").val(contents); }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="block"> <div class="phone"><span>+1234567</span></div> <div class="email"> <span>email@email.com</span> <button>CLick</button> </div> </div> <div class="block"> <div class="phone"><span>+7696686</span></div> <div class="email"> <span>another@email.com</span> <button>CLick</button> </div> </div> <div class="block"> <div class="phone"><span>+897979877</span></div> <div class="email"> <span>email2@email.com</span> <button>CLick</button> </div> </div> <input type="text" value="" id="phone">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.