gpt4 book ai didi

jquery - 如何使用jquery显示select语句中选项标签内的javascript变量的值?

转载 作者:行者123 更新时间:2023-12-01 03:08:09 25 4
gpt4 key购买 nike

我已经获得了两个 JavaScript 变量的值。我想在 value 属性的选项标签内给出。当我想在 select 语句中检索该选项的值时,如何在 jQuery 中给出该值?

现在我已经给了它。但它显示的是变量而不是值。

var packagetype = item.package_type;
var package_price = item.package_price;


$("select.selectedpackagetype").append("<option class='firstoption' value='packagetype:package_price'> "+packagetype+" - Rs. "+package_price+"</option>");

最佳答案

Now i have current given it. but it is displaying the variable not the value.

嗯,当然是这样。您已将文本 packagetype:package_price 按字面意思放入字符串内。在同一行的其他地方,您使用字符串连接,这也是您可能用于 value 的内容:

$("select.selectedpackagetype").append("<option class='firstoption' value='" + packagetype + ":" + package_price + "'> "+packagetype+" - Rs. "+package_price+"</option>");
// ------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

但我不会使用字符串连接来构建该选项的 HTML;我将构建选项对象,然后附加它:

var packagetype = item.package_type;
var package_price = item.package_price;

$("<option>")
.val(packagetype + ":" + package_price)
.addClass("firstoption")
.text(packagetype + " - Rs. " + package_price)
.appendTo("select.selectedpackagetype");

示例:

var packagetype = "TheType";
var package_price = 42;

$("<option>")
.val(packagetype + ":" + package_price)
.addClass("firstoption")
.text(packagetype + " - Rs. " + package_price)
.appendTo("select.selectedpackagetype");
<select class="selectedpackagetype"></select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于jquery - 如何使用jquery显示select语句中选项标签内的javascript变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40544737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com