简体   繁体   English

如何将此输入名称转换为正确的标签格式

[英]how do i turn this input name to the correct label format

I have this name on my input 我在输入中输入了这个名字

driver_application[company_name]

and i need to create the label name to be 我需要创建标签名称

driver_application_company_name

Here is my jquery to get the input 这是我的jQuery来获取输入

$(".company_name").attr("name") 

will return 将返回

driver_application[company_name]

how do i convert it to the right format with javascript or jquery ...maybe regex 如何使用javascript或jquery将其转换为正确的格式...也许正则表达式

You can use the Javascript method replace : 您可以使用Javascript方法replace

var source = 'driver_application[company_name]';
var formatted = source.replace('[', '_').replace(']', '');
console.log(formatted); //prints driver_application_company_name

您是否正在寻找这样的东西?

'driver_application[company_name]'.replace('[', '_').replace(']', '');

从exisitng输入获取名称以用于其他元素

var labelName=$(".company_name").attr("name").replace('[','_').replace(']','');

try this 尝试这个

$(".company_name").attr("name","driver_application_company_name")

http://api.jquery.com/attr/ http://api.jquery.com/attr/

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

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