首页 > 解决方案 > 选择所有具有名称属性的输入包含子字符串 - jQuery

问题描述

我想选择所有名称属性以 . 结尾的输入*Name

我有 6 个输入类型文本,名称为:

HTML

<input class="form-control" type="text" name="deviceName">
<input class="form-control" type="text" name="profileName">
<input class="form-control" type="text" name="ssidName">
<input class="form-control" type="text" name="captiveName">
<input class="form-control" type="text" name="trafficName">
<input class="form-control" type="text" name="sessionName">

我试图防止在这 5 个输入上输入任何空格

我试过了

$("input[name='*Name']").keyup(function() {
    this.value = this.value.replace(/\s/g, '');
});

我的选择似乎没有任何影响。

标签: javascriptjquery

解决方案


我认为您使用了正确的选择器,但在错误的位置。

你需要像这样使用它$("input[name*='Name']")

请参阅 jquery 文档的链接。https://api.jquery.com/attribute-contains-selector/


推荐阅读