select2.js 获取事件目标
select2.js get event target
我有两个 <select>
元素,两者的初始化相同。
但是他们的ids
不同,我想在初始化里面访问他们的id
属性。
<select name="names[]" id="user_name" class="tags required_field" style="width: 150px;">
<option>Rakesh Malakar</option>
<option>John Wick</option>
</select>
<script type="text/javascript">
<select name="emails[]" id="user_email" class="tags required_field" style="width: 200px;">
<option>malakar_rakesh@gmail.com</option>
<option>johnwich@yahoo.com</option>
</select>
$(".tags").select2({
tags: true,
placeholder: event.target.id == 'user_name' ? 'Select Name' : 'Select Email', // something like this
language: {
noResults: function() {
return 'Type and enter to add new';
},
},
escapeMarkup: function(markup) {
return markup;
},
});
</script>
像这样的东西应该可以工作:
$(".tags").each(function() {
var placeholder = "Select Email";
if ($(this).attr('id') === 'user_name') {
placeholder = "Select Name";
}
$(this).select2({
tags: true,
placeholder: placeholder
language: {
noResults: function() {
return 'Type and enter to add new';
},
},
escapeMarkup: function(markup) {
return markup;
},
});
});
我有两个 <select>
元素,两者的初始化相同。
但是他们的ids
不同,我想在初始化里面访问他们的id
属性。
<select name="names[]" id="user_name" class="tags required_field" style="width: 150px;">
<option>Rakesh Malakar</option>
<option>John Wick</option>
</select>
<script type="text/javascript">
<select name="emails[]" id="user_email" class="tags required_field" style="width: 200px;">
<option>malakar_rakesh@gmail.com</option>
<option>johnwich@yahoo.com</option>
</select>
$(".tags").select2({
tags: true,
placeholder: event.target.id == 'user_name' ? 'Select Name' : 'Select Email', // something like this
language: {
noResults: function() {
return 'Type and enter to add new';
},
},
escapeMarkup: function(markup) {
return markup;
},
});
</script>
像这样的东西应该可以工作:
$(".tags").each(function() {
var placeholder = "Select Email";
if ($(this).attr('id') === 'user_name') {
placeholder = "Select Name";
}
$(this).select2({
tags: true,
placeholder: placeholder
language: {
noResults: function() {
return 'Type and enter to add new';
},
},
escapeMarkup: function(markup) {
return markup;
},
});
});