英文:
Trying to populate a form's subject line with HTML class
问题
我知道有更好的方法来实现这个,但我目前正在尝试使用jQuery来将弹出式电子邮件表单的主题行填充为当前元素的给定类名。我已经成功使它可以正确填充,但它正在倾倒该类别中的所有名称,而我只想获取嵌套在按钮中的特定名称:
var carname = $('.car-name').text();
$('#email-subject').val(carname);
这就是jQuery的代码,如何才能让它只提取特定元素的名称,而不是所有元素的名称呢?
谢谢!
编辑:附加的HTML信息:
<div class="featuredcar">
<img src="images/cars/1065_00.394180.jpg">
<div class="featuredinfo">
<h5 class="car-name">1968 Mustang</h5>
<p>$10,000</p>
<p><a href="#feat3" id="feat3">More info...</a></p>
<p class="contact-p">
<div class="contact-car">
<i class="fas fa-envelope"></i> 联系我们
</div>
about this car!
</p>
</div>
</div>
<div id="contact-car-form">
<span id="close-form"><a href="javascript:void(0)" onclick="closeForm()">×</a></span>
<h2>想了解更多信息?</h2>
<small>我会尽快回复您</small>
<form action="#">
<input placeholder="姓名" type="text" required />
<input placeholder="电子邮件" type="email" required />
<input id="email-subject" placeholder="主题" type="text" required />
</form>
</div>
我需要将
<input id="email-subject" placeholder="主题" type="text" required />
的值填充为
<h5 class="car-name">1968 Mustang</h5>
希望这能消除任何困惑。如果还有其他信息需要提供,请告诉我,感谢您的所有帮助!
英文:
I know there are better ways to do this, but I'm currently trying to use Jquery to populate my pop-up email form's subject line to populate with the given class name of the current element. I've gotten it to the point where it will populate properly, but it is dumping in ALL of the names using that class, when i only want it to pull the specific one in which the button is nested:
var carname = $('.car-name').text();
$('input[id="email-subject"]').val(carname);
that is the Jquery of it, how can i get it so it only pulls that specific element's name vs. all of them?
thanks!
EDIT: Additional HTML info:
<div class="featuredcar">
<img src="images/cars/1065_00.394180.jpg">
<div class="featuredinfo">
<h5 class="car-name">1968 Mustang</h5>
<p>$10,000</p>
<p><a href="#feat3" id="feat3">More info...</a></p>
<p class="contact-p">
<div class="contact-car">
<i class="fas fa-envelope"></i>&nbsp;Contact Us</div> about this car!</p>
</div>
</div>
<div id="contact-car-form">
<span id="close-form"><a href="javascript:void(0)" onclick="closeForm()">&times;</a></span>
<h2>Want more Info?</h2>
<small>I'll get back to you as quickly as possible</small>
<form action="#">
<input placeholder="Name" type="text" required />
<input placeholder="Email" type="email" required />
<input id="email-subject" placeholder="Subject" type="text" required />
I need the
input id="email-subject"
to be populated with the text value from
<h5 class="car-name">1968 Mustang</h5>
hopefully, this clears up any confusion. let me know if there is anything else I can provide and thank you for all of your help!
答案1
得分: 1
这段代码将返回按钮父元素的类名。在我的示例中,类名是'test':
$(“.btn”).on('click',function(event){
var carname = $(this).parent()。attr('class');
$('input[id="email-subject"]').val(carname);
});
<div class="test">
<button class="btn">Click</button>
</div>
英文:
This code will return the class name of the button's parent element. Which is 'test' in my example:
$(".btn").on('click', function(event){
var carname = $(this).parent().attr('class');
$('input[id="email-subject"]').val(carname);
});
<div class="test">
<button class="btn">Click</button>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论