[英]How to get “ID” value from “name[ID][OTHER]” in attribute of element using jquery?
[英]How to get a value of an element by name instead of ID
如何通過屬性name
而不是ID
獲取元素的值。 例如,如果我按 id 使用,它將是$('#id').val();
使用名稱屬性選擇器:
$("input[name=nameGoesHere]").val();
$('[name=whatever]').val()
jQuery文檔是您的朋友。
使用名稱屬性選擇器:
$("input[name='nameGoesHere']").val();
在值周圍包含引號是強制性要求,請參閱: http : //api.jquery.com/attribute-equals-selector/
要獲取值,我們可以使用多個屬性,其中之一是name屬性。 例如
$("input[name='nameOfElement']").val();
我們還可以使用其他屬性來獲取值
HTML
<input type="text" id="demoText" demo="textValue" />
JS
$("[demo='textValue']").val();
這工作正常..這里 btnAddCat 是按鈕 id
$('#btnAddCat').click(function(){
var eventCategory=$("input[name=txtCategory]").val();
alert(eventCategory);
});
//name directly given
<input type="text" name="MeetingDateFrom">
var meetingDateFrom = $("input[name=MeetingDateFrom]").val();
//Handle name array
<select multiple="multiple" name="Roles[]"></select>
var selectedValues = $('select[name="Roles[]"] option:selected').map(function() {
arr.push(this.value);
});
還有一件事:當您使用“crasis 變量賦值”時,您也需要使用雙 cotes 並且您不需要使用“輸入”詞!:
valInput = $(`[name="${inputNameHere}"]`).val();
let startDate = $('[name=daterangepicker_start]').val();
alert(startDate);
綠色背景部分使用什么元素? 只需鍵入不帶“<”和“>”字符的元素名稱。 例如,如果答案是<P>
元素,則輸入 P,而不是<P>
。
你也可以使用類名。
$(".yourclass").val();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.