我的印象是,我可以通过执行这个$(this).val()来获取一个选择输入的值;并将onchange参数应用到select字段。
只有在引用ID时,它才会起作用。
我怎么用这个。
我的印象是,我可以通过执行这个$(this).val()来获取一个选择输入的值;并将onchange参数应用到select字段。
只有在引用ID时,它才会起作用。
我怎么用这个。
当前回答
让我分享一个我用BS4、thymleaf和Spring boot开发的例子。
我使用了两个select,其中第二个(“subtopic”)由基于第一个(“topic”)选择的AJAX调用填充。
首先,百里叶片段:
<div class="form-group">
<label th:for="topicId" th:text="#{label.topic}">Topic</label>
<select class="custom-select"
th:id="topicId" th:name="topicId"
th:field="*{topicId}"
th:errorclass="is-invalid" required>
<option value="" selected
th:text="#{option.select}">Select
</option>
<optgroup th:each="topicGroup : ${topicGroups}"
th:label="${topicGroup}">
<option th:each="topicItem : ${topics}"
th:if="${topicGroup == topicItem.grp} "
th:value="${{topicItem.baseIdentity.id}}"
th:text="${topicItem.name}"
th:selected="${{topicItem.baseIdentity.id==topicId}}">
</option>
</optgroup>
<option th:each="topicIter : ${topics}"
th:if="${topicIter.grp == ''} "
th:value="${{topicIter.baseIdentity.id}}"
th:text="${topicIter.name}"
th:selected="${{topicIter.baseIdentity?.id==topicId}}">
</option>
</select>
<small id="topicHelp" class="form-text text-muted"
th:text="#{label.topic.tt}">select</small>
</div><!-- .form-group -->
<div class="form-group">
<label for="subtopicsId" th:text="#{label.subtopicsId}">subtopics</label>
<select class="custom-select"
id="subtopicsId" name="subtopicsId"
th:field="*{subtopicsId}"
th:errorclass="is-invalid" multiple="multiple">
<option value="" disabled
th:text="#{option.multiple.optional}">Select
</option>
<option th:each="subtopicsIter : ${subtopicsList}"
th:value="${{subtopicsIter.baseIdentity.id}}"
th:text="${subtopicsIter.name}">
</option>
</select>
<small id="subtopicsHelp" class="form-text text-muted"
th:unless="${#fields.hasErrors('subtopicsId')}"
th:text="#{label.subtopics.tt}">select</small>
<small id="subtopicsIdError" class="invalid-feedback"
th:if="${#fields.hasErrors('subtopicsId')}"
th:errors="*{subtopicsId}">Errors</small>
</div><!-- .form-group -->
我对存储在模型上下文中的主题列表进行迭代,显示所有组及其主题,然后显示所有没有组的主题。 顺便说一句,BaseIdentity是@Embedded复合键。
现在,这里是处理更改的jQuery:
$('#topicId').change(function () {
selectedOption = $(this).val();
if (selectedOption === "") {
$('#subtopicsId').prop('disabled', 'disabled').val('');
$("#subtopicsId option").slice(1).remove(); // keep first
} else {
$('#subtopicsId').prop('disabled', false)
var orig = $(location).attr('origin');
var url = orig + "/getsubtopics/" + selectedOption;
$.ajax({
url: url,
success: function (response) {
var len = response.length;
$("#subtopicsId option[value!='']").remove(); // keep first
for (var i = 0; i < len; i++) {
var id = response[i]['baseIdentity']['id'];
var name = response[i]['name'];
$("#subtopicsId").append("<option value='" + id + "'>" + name + "</option>");
}
},
error: function (e) {
console.log("ERROR : ", e);
}
});
}
}).change(); // and call it once defined
change()的初始调用确保它将在页面重新加载时执行,或者在后端初始化时预选了某个值时执行。
顺便说一句:我正在使用“手动”表单验证(参见“is-valid”/“is-invalid”),因为我(和用户)不喜欢BS4将非必需的空字段标记为绿色。但这超出了这个问题的范围,如果你感兴趣,那么我也可以张贴它。
其他回答
对于所有选择,调用此函数。
$('select').on('change', function()
{
alert( this.value );
});
只有一个选择:
$('#select_id')
箭头函数的作用域与函数不同, 这一点。Value将为箭头函数提供undefined。 固定使用
$('select').on('change',(event) => {
alert( event.target.value );
});
让我分享一个我用BS4、thymleaf和Spring boot开发的例子。
我使用了两个select,其中第二个(“subtopic”)由基于第一个(“topic”)选择的AJAX调用填充。
首先,百里叶片段:
<div class="form-group">
<label th:for="topicId" th:text="#{label.topic}">Topic</label>
<select class="custom-select"
th:id="topicId" th:name="topicId"
th:field="*{topicId}"
th:errorclass="is-invalid" required>
<option value="" selected
th:text="#{option.select}">Select
</option>
<optgroup th:each="topicGroup : ${topicGroups}"
th:label="${topicGroup}">
<option th:each="topicItem : ${topics}"
th:if="${topicGroup == topicItem.grp} "
th:value="${{topicItem.baseIdentity.id}}"
th:text="${topicItem.name}"
th:selected="${{topicItem.baseIdentity.id==topicId}}">
</option>
</optgroup>
<option th:each="topicIter : ${topics}"
th:if="${topicIter.grp == ''} "
th:value="${{topicIter.baseIdentity.id}}"
th:text="${topicIter.name}"
th:selected="${{topicIter.baseIdentity?.id==topicId}}">
</option>
</select>
<small id="topicHelp" class="form-text text-muted"
th:text="#{label.topic.tt}">select</small>
</div><!-- .form-group -->
<div class="form-group">
<label for="subtopicsId" th:text="#{label.subtopicsId}">subtopics</label>
<select class="custom-select"
id="subtopicsId" name="subtopicsId"
th:field="*{subtopicsId}"
th:errorclass="is-invalid" multiple="multiple">
<option value="" disabled
th:text="#{option.multiple.optional}">Select
</option>
<option th:each="subtopicsIter : ${subtopicsList}"
th:value="${{subtopicsIter.baseIdentity.id}}"
th:text="${subtopicsIter.name}">
</option>
</select>
<small id="subtopicsHelp" class="form-text text-muted"
th:unless="${#fields.hasErrors('subtopicsId')}"
th:text="#{label.subtopics.tt}">select</small>
<small id="subtopicsIdError" class="invalid-feedback"
th:if="${#fields.hasErrors('subtopicsId')}"
th:errors="*{subtopicsId}">Errors</small>
</div><!-- .form-group -->
我对存储在模型上下文中的主题列表进行迭代,显示所有组及其主题,然后显示所有没有组的主题。 顺便说一句,BaseIdentity是@Embedded复合键。
现在,这里是处理更改的jQuery:
$('#topicId').change(function () {
selectedOption = $(this).val();
if (selectedOption === "") {
$('#subtopicsId').prop('disabled', 'disabled').val('');
$("#subtopicsId option").slice(1).remove(); // keep first
} else {
$('#subtopicsId').prop('disabled', false)
var orig = $(location).attr('origin');
var url = orig + "/getsubtopics/" + selectedOption;
$.ajax({
url: url,
success: function (response) {
var len = response.length;
$("#subtopicsId option[value!='']").remove(); // keep first
for (var i = 0; i < len; i++) {
var id = response[i]['baseIdentity']['id'];
var name = response[i]['name'];
$("#subtopicsId").append("<option value='" + id + "'>" + name + "</option>");
}
},
error: function (e) {
console.log("ERROR : ", e);
}
});
}
}).change(); // and call it once defined
change()的初始调用确保它将在页面重新加载时执行,或者在后端初始化时预选了某个值时执行。
顺便说一句:我正在使用“手动”表单验证(参见“is-valid”/“is-invalid”),因为我(和用户)不喜欢BS4将非必需的空字段标记为绿色。但这超出了这个问题的范围,如果你感兴趣,那么我也可以张贴它。
$('#select-id').change(function() {
console.log($(this).val());
});
这对我有帮助。
选择:
$('select_tags').on('change', function() {
alert( $(this).find(":selected").val() );
});
For radio / checkbox:
$('radio_tags').on('change', function() {
alert( $(this).find(":checked").val() );
});