I have got the values of two JavaScript variables. I wanted to give inside the option tag in the value attribute. How to give that in jQuery as I wanted to retrieve the value of that option in the select statement?
我有两个JavaScript变量的值。我想在value属性中给出option标签。如何在jQuery中给出它,因为我想在select语句中检索该选项的值?
Now I have currently given it. but it is displaying the variable not the value.
现在我已经给了它。但它显示的是变量而不是值。
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>");
2 个解决方案
#1
4
Now i have current given it. but it is displaying the variable not the value.
现在我有了它。但它显示的是变量而不是值。
Well, of course it is. You've put the text packagetype:package_price
literally inside the string. Elsewhere in that same line you use string concatenation, which is also what you might use for value
:
嗯,当然是。你把文本packagetype:package_price字面地放在字符串中。在同一行的其他地方,您使用字符串连接,这也是您可能用于值的内容:
$("select.selectedpackagetype").append("<option class='firstoption' value='" + packagetype + ":" + package_price + "'> "+packagetype+" - Rs. "+package_price+"</option>");
// ------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
But I wouldn't use string concatenation to build HTML for the option; I'd build the option object and then append it:
但我不会使用字符串连接来为该选项构建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");
Example:
例:
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>
#2
0
You were very close.
你非常接近。
$("select.selectedpackagetype").append("<option class='firstoption' value='" + packagetype + ":" + package_price+ "'> "+packagetype+" - Rs. "+package_price+"</option>");
But T.J Crowders technique is better if you had the time to change your approach.
但是如果你有时间改变你的方法,T.J Crowders技术会更好。
#1
4
Now i have current given it. but it is displaying the variable not the value.
现在我有了它。但它显示的是变量而不是值。
Well, of course it is. You've put the text packagetype:package_price
literally inside the string. Elsewhere in that same line you use string concatenation, which is also what you might use for value
:
嗯,当然是。你把文本packagetype:package_price字面地放在字符串中。在同一行的其他地方,您使用字符串连接,这也是您可能用于值的内容:
$("select.selectedpackagetype").append("<option class='firstoption' value='" + packagetype + ":" + package_price + "'> "+packagetype+" - Rs. "+package_price+"</option>");
// ------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
But I wouldn't use string concatenation to build HTML for the option; I'd build the option object and then append it:
但我不会使用字符串连接来为该选项构建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");
Example:
例:
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>
#2
0
You were very close.
你非常接近。
$("select.selectedpackagetype").append("<option class='firstoption' value='" + packagetype + ":" + package_price+ "'> "+packagetype+" - Rs. "+package_price+"</option>");
But T.J Crowders technique is better if you had the time to change your approach.
但是如果你有时间改变你的方法,T.J Crowders技术会更好。