我有以下HTML标记:
<select class="form-control travel_mode" name="travel_request[travel_stays_attributes][0][travel_mode]" style="width: 139px;">
<option value=""></option>
<option disabled="true" selected="selected">Please Select</option>
<option value="1">Flight</option>
</select>
<select class="form-control travel_mode" name="travel_request[travel_stays_attributes][1448001168204][travel_mode]" style="width: 139px;">
<option value=""></option>
<option disabled="true" selected="selected">Please Select</option>
<option value="1">Flight</option>
</select>
<select class="form-control travel_mode" name="travel_request[travel_stays_attributes][1448002208899][travel_mode]" style="width: 139px;">
<option value=""></option>
<option disabled="true" selected="selected">Please Select</option>
<option value="1">Flight</option>
</select>
<select class="form-control travel_mode" name="travel_request[travel_stays_attributes][1448002215357][travel_mode]" style="width: 139px;">
<option value=""></option>
<option disabled="true" selected="selected">Please Select</option>
<option value="1">Flight</option>
</select>
我想通过编写一个正则表达式来对名称属性上的这些元素调用onclick
或onchange
函数,以便在单击上述任何一个下拉列表时调用单个JS函数。如何为上述标记编写正则表达式?
答案 0 :(得分:0)
您不需要正则表达式;所有元素都有class
,因此您可以使用:
$('select.form-control').change(function() {
console.log('I changed value...');
});
如果您想使其更具针对这些元素,可以使用attribute starts with选择器中的name
属性:
$('select[name^="travel_request[travel_stays_attributes]"]').change(function() {
console.log('I changed value...');
});