我有一個輸入名稱是formComponentsMap['order'].firstName
我將如何針對jQuery中的特定輸入?我如何使用[和。在它使用jQuery?
全部輸入HTML
<input name="formComponentsMap['order'].firstName" type="text" value="THOMAS" maxlength="20" class="form-control">
我有一個輸入名稱是formComponentsMap['order'].firstName
我將如何針對jQuery中的特定輸入?我如何使用[和。在它使用jQuery?
全部輸入HTML
<input name="formComponentsMap['order'].firstName" type="text" value="THOMAS" maxlength="20" class="form-control">
你可以使這樣的事情,使用轉義字符 「\\」
你可以閱讀更多關於jQuery docs
escpaing在jQuery選擇特殊字符$(function() {
console.log($("input[name='formComponentsMap[\\'order\\'].firstName']").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="formComponentsMap['order'].firstName" type="text" value="THOMAS" maxlength="20" class="form-control">
這工作完美!謝謝!在這種情況下使用^是因爲「下面提到的ryan?再次感謝所有人! –
對不起,我的壞。^不是必需的,但它仍然有效,因爲它匹配名稱以」formComponentsMap [\\ 'order \\']。firstName']「。 – Sreekanth
你需要做的:
$("input[name=\"formComponentsMap['order'].firstName\"").val();
與麻煩的是,第二對""
的將難逃第一,有效地切斷選擇一半,並努力的目標input[name=\
。
斯里卡特的答案特殊字符轉義可能是最好的,但另一種解決方案是,爲輸入ID和做:
<input id="myInput"name="formComponentsMap['order'].firstName" type="text" value="THOMAS" maxlength="20" class="form-control">
和
$("#myInput").val();
在這種情況下更改html wasnt選項:/ –
閱讀文檔。 https://api.jquery.com/category/selectors/說明如何在頁面頂部正確執行此操作。 – epascarello