在 select2 中使用 ajax 时无法获取自定义属性值
can not get customized attribute value when using ajax in select2
我正在使用 ASP.NET
I have a dropdown using select2 and an (apicontroller) action with two parameters is called using ajax when something is selected.
动作:
public class MyController: ApiController
{
[HttpGet]
public string Helper(string para0, string para1, string para2) { return something;}
}
html:
<select id="selectCase">
<option>Select a Case</option>
<option value="abc" data-para1="1" data-para2="2"> "abc" </option>
<option value="def" data-para1="1" data-para2="2"> "abc" </option>
</select>
js函数:
$('#selectCase').on('select2:select', function() {
$.ajax({
type: "GET",
url: "/api/MyController/Helper?para0=" + $(this).val() + "¶1=" + $(this).attr("data-para1") + "¶2=" + $(this).attr("data-para1"),
但是运行时只有$(this).val()得到正确的值,$(this).attr("data-para1"和$(this).attr("data-para1"是两者都未定义。还尝试使用 $(this).data("para1"),但仍然无法正常工作。
谢谢,
我实际上跳过了我在 html 中使用 optgroup 的部分,不确定这是否有什么不同
<select id="selectCase">
<option></option>
<optgroup label="0">
<option value="abc" data-para1="1" data-para2="2"> "abc" </option>
</optgroup>
<optgroup label="0">
<option value="def" data-para1="1" data-para2="2"> "abc" </option>
</optgroup>
</select>
您可以使用 $(this).find('option:selected')
获取选定的选项,然后获取您在每个选项中定义的自定义属性。
演示代码 :
$('#selectCase').select2({
width: '100%',
placeholder: "Select an Option",
allowClear: true
});
$('#selectCase').on('select2:select', function() {
//getting selected option and then retrieve required data
var $select = $(this).find('option:selected');
console.log("para0=" + $select.val() + "¶1=" + $select.attr('data-para1') + "¶2=" + $select.attr('data-para2'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet" />
<select id="selectCase">
<option></option>
<optgroup label="0">
<option value="abc" data-para1="1" data-para2="2"> "abc" </option>
</optgroup>
<optgroup label="0">
<option value="def" data-para1="1" data-para2="2"> "abc" </option>
</optgroup>
</select>
我正在使用 ASP.NET
I have a dropdown using select2 and an (apicontroller) action with two parameters is called using ajax when something is selected.
动作:
public class MyController: ApiController
{
[HttpGet]
public string Helper(string para0, string para1, string para2) { return something;}
}
html:
<select id="selectCase">
<option>Select a Case</option>
<option value="abc" data-para1="1" data-para2="2"> "abc" </option>
<option value="def" data-para1="1" data-para2="2"> "abc" </option>
</select>
js函数:
$('#selectCase').on('select2:select', function() {
$.ajax({
type: "GET",
url: "/api/MyController/Helper?para0=" + $(this).val() + "¶1=" + $(this).attr("data-para1") + "¶2=" + $(this).attr("data-para1"),
但是运行时只有$(this).val()得到正确的值,$(this).attr("data-para1"和$(this).attr("data-para1"是两者都未定义。还尝试使用 $(this).data("para1"),但仍然无法正常工作。
谢谢,
我实际上跳过了我在 html 中使用 optgroup 的部分,不确定这是否有什么不同
<select id="selectCase">
<option></option>
<optgroup label="0">
<option value="abc" data-para1="1" data-para2="2"> "abc" </option>
</optgroup>
<optgroup label="0">
<option value="def" data-para1="1" data-para2="2"> "abc" </option>
</optgroup>
</select>
您可以使用 $(this).find('option:selected')
获取选定的选项,然后获取您在每个选项中定义的自定义属性。
演示代码 :
$('#selectCase').select2({
width: '100%',
placeholder: "Select an Option",
allowClear: true
});
$('#selectCase').on('select2:select', function() {
//getting selected option and then retrieve required data
var $select = $(this).find('option:selected');
console.log("para0=" + $select.val() + "¶1=" + $select.attr('data-para1') + "¶2=" + $select.attr('data-para2'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet" />
<select id="selectCase">
<option></option>
<optgroup label="0">
<option value="abc" data-para1="1" data-para2="2"> "abc" </option>
</optgroup>
<optgroup label="0">
<option value="def" data-para1="1" data-para2="2"> "abc" </option>
</optgroup>
</select>