JQuery的类选择器包含大括号作为字符串

时间:2019-02-02 09:16:58

标签: javascript php jquery css

我有一个类名,例如“ toxics_decompose(phase)_field1”。

每当我尝试在JQUERY中通过此类名称获取数据时,都会抛出错误

HTML

<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required ></textarea>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required ></textarea>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required ></textarea>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required ></textarea>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required ></textarea>
OR
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required >
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required >
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required >
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required >
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required >

JQUERY

$(".toxics_decompose(phase)_field1").each(function(i,obj){
    console.log($(this).val());
});

//OR

 [SEC.1] var string=$('input[name^="toxics_decompose(phase)_field1[]"]').serialize();

但是[SEC.1]中没有确定输入类型是文本区域还是数字,因此它如何工作。 注意:此处[SEC.1]仅用于参考。

$(".toxics_decompose(phase)_field1").each(function(i, obj) {
  console.log($(this).val());
});

var string = $('input[name^="toxics_decompose(phase)_field1[]"]').serialize();

console.log(string)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required>wer</textarea>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required>wer</textarea>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required>wer</textarea>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required></textarea>
<textarea rows="4" cols="10" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required></textarea> OR
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required value="asd">
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required>
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required>
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required>
<input type="number" class="form-control toxics_decompose(phase)_field1" name="toxics_decompose(phase)_field1[]" placeholder="" required>

在我的方案中,我使用的是数据库获取的名称“ toxics_decompose(phase)_field1”,它会根据更改而有所不同。

0 个答案:

没有答案