[英]Not able to append options to select
I am not sure why this works but not when I pass in numbers 我不确定为什么这样做,但是当我输入数字时却不知道
<form id="form1" runat="server">
<div id="facebookPhotos-iFrameContent">
<div>
<p>Log in</p>
<p id="LoginButtonContainer"><input type="image" id="btnLogin" src="images/loginBtn.jpg" /></p>
<p><select id="facebookAlbumDropdown" /></p>
<div id="facebookPhotosContainer" />
</div>
</div>
</form>
AddDropdownItem("-Select something test-", "-1", "testDropdown");
function AddDropdownItem(sText, sValue, sDropdownID)
{
$("#" + sDropdownID).append("<option value=" + sValue + ">" + sText + "</option>");
}
that works 这样可行
but this does not: 但这不是:
var id = 104388426283811;
AddDropdownItem("-Select something test-", id.toString(), "testDropdown");
What happens is the default option shows up but then when it tries to add the second option it bombs out with no errors that I can see in the firebug console and then in the end the list goes blank (no options) when my code is done running. 发生的是显示默认选项,但是当它尝试添加第二个选项时,它轰炸了而没有在Firebug控制台中看到的错误,最后,当我的代码完成时,列表变成空白(没有选项)运行。
JavaScript会在需要的地方将整数解释为字符串,而无需使用toString()
。
Try putting the value in single quotes, like this: 尝试将值放在单引号中,如下所示:
function AddDropdownItem(sText, sValue, sDropdownID)
{
$("#" + sDropdownID).append("<option value='" + sValue + "'>" + sText + "</option>");
}
You asked, "Why would that matter." 您问,“为什么会这样。” The answer.
答案。 It is a good practice and it prevents problems when your values start having spaces in them.
这是一个好习惯,可以防止您的值开始出现空格时出现问题。 I ALWAYS put attribute values in quotes.
我总是将属性值放在引号中。
Now, for your problem... I just tried it with the following code and it works like a charm! 现在,为您的问题...我只是用以下代码尝试了一下,它的工作原理就像一个魅力!
<p><select name="MyTestDropdown" id="testDropdown"></select></p>
<script type="text/javascript">
$(document).ready(function () {
AddDropdownItem("-Select something test-", "-1", "testDropdown");
AddDropdownItem("something else", "1", "testDropdown");
AddDropdownItem("another thing", 2, "testDropdown");
var id = 104388426283811;
AddDropdownItem("test big value", id.toString(), "testDropdown");
AddDropdownItem("Profile Pictures", 100001379631246, "testDropdown");
AddDropdownItem("Test Test2", 104388426283811, "testDropdown");
});
function AddDropdownItem(sText, sValue, sDropdownID)
{
$("#" + sDropdownID).append("<option value='" + sValue + "'>" + sText + "</option>");
}
</script>
Your original code actually works, too. 您的原始代码实际上也有效。 I don't think there was a problem with it.
我认为这没有问题。 :) Maybe you have a problem elsewhere in your code?
:)也许您在代码的其他地方有问题?
Based on the comment thread in the original question, here's my sample page that incorporates your logic (along with wiring up a button to add options to the select): 根据原始问题中的评论主题,这是我的示例页面,其中包含您的逻辑(以及连接按钮以向选项中添加选项):
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
$(document).ready(function() {
$("#btnClick").click(function(e) {
e.preventDefault();
var id = 104388426283811;
AddDropdownItem("-Select something test-", id.toString(), "testDropdown");
//AddDropdownItem("-Select something test-", "-1", "testDropdown");
});
});
function AddDropdownItem(sText, sValue, sDropdownID)
{
$("#" + sDropdownID).append("<option value='" + sValue + "'>" + sText + "</option>");
}
</script>
</head>
<body>
<input id='btnClick' type='submit' value='click'></input>
<p><select name="MyTestDropdown" id="testDropdown"></select></p>
</body>
</html>
I don't have the iframes that you mentioned, but I'm using an input submit
element to add items in. I'm preventing the default behavior of a submit button by calling e.preventDefault();
我没有您提到的iframe,但是我正在使用
input submit
e.preventDefault();
元素添加项目。我通过调用e.preventDefault();
防止e.preventDefault();
按钮的默认行为e.preventDefault();
, which then prevents the post back. ,这样可以防止发回邮件。 I'm then able to add items to the
select
element. 然后,我可以将项目添加到
select
元素。
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.