[英]Jquery get value and display in span
我有一個帶有幾個選項的選擇字段,每個選項都分配了“值”屬性,並且有名稱。 選擇其中一個選項后,我希望其中一個充滿標題,第二個充滿值。 標題可以正常工作,但是我無法捕捉到分配的“ value =“ asd”“值。
$(".itemclass").on("change", function () {
$("span.iclass").text(this.options[this.selectedIndex].textContent);
$("span.impl").text(this.options[this.selectedIndex].val());
});
我想念什么?
您可以通過以下方式訪問選定的選項:
$(".itemclass").on("change", function () {
var selectedOption = $(this).find("option:selected");
$("span.iclass").text(selectedOption.text());
$("span.impl").text(selectedOption.val());
});
或者,如果您更喜歡使用DOM節點,則:
$(".itemclass").on("change", function () {
var selectedOption = $(this).find("option:selected").get(0);
$("span.iclass").text(selectedOption.textContent);
$("span.impl").text(selectedOption.value);
});
用這個:
$(".itemclass").on("change", function () {
$("span.iclass").text(this.options[this.selectedIndex].textContent);
$("span.impl").text($(this).val());
});
您可以嘗試使用以下代碼:
$(".itemclass").on("change", function () {
$("span.iclass").text($(this).find("option:selected").text());
$("span.impl").text($(this).find("option:selected").val());
});
好吧,我對雙關語做了些擺弄。
這是我想出的。
$(".itemclass").change(function()
{
$("#Name").text(this.options[this.selectedIndex].textContent);
$("#Value").text(this.options[this.selectedIndex].value);
});
這是我的小提琴
這是我找到您解決方案的地方,請查看此問題的答案以了解原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.