gpt4 book ai didi

javascript - 如何从复杂属性中获取变量?

转载 作者:行者123 更新时间:2023-12-01 05:39:11 26 4
gpt4 key购买 nike

HTML 看起来像这样:

<input class="car-type-input" name="carType[bmw]".../>

如何使用 jQuery 在 javascript 中获取包含 bmw 的变量?

Html 无法更改!

当前 JavaScript:

  $('.car-type-input').each(function() {
if ($(this).is(':checked')) {
var carType = $(this).attr('name')..// Get the variable
alert(carType);
}

最佳答案

您可以使用正则表达式,例如

$('.car-type-input').each(function() {
var name = this.name;
var regex = name.match(/\[(.*?)\]/)[1];
var substring = name.substring(8, name.length - 1)
snippet.log('regex: ' + regex);
snippet.log('substring: ' + substring);
})
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input class="car-type-input" name="carType[bmw]" .../>

关于javascript - 如何从复杂属性中获取变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31936014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com