Javascript 无法附加选项以选择

Javascript 无法附加选项以选择,javascript,jquery,Javascript,Jquery,我不知道为什么这样做有效,但当我通过数字时就不行了 <form id="form1" runat="server"> <div id="facebookPhotos-iFrameContent"> <div> <p>Log in</p> <p id="LoginButtonContainer"><input type="image" id="btn

我不知道为什么这样做有效,但当我通过数字时就不行了

<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>

默认选项会出现,但当它尝试添加第二个选项时,它会在firebug控制台中爆炸出来,没有我能看到的错误,然后在代码运行结束时,列表变为空白(没有选项)。

尝试将值放在单引号中,如下所示:

function AddDropdownItem(sText, sValue, sDropdownID)
{
      $("#" + sDropdownID).append("<option value='" + sValue + "'>" + sText + "</option>");
}
函数AddDropdownItem(sText、sValue、sDropdownID)
{
$(“#”+sDropdownID).append(“+sText+”);
}
你问,“那有什么关系。”答案是。这是一个很好的实践,它可以防止在值中开始有空格时出现问题。我总是用引号引属性值

现在,为了你的问题。。。我只是用下面的代码试了一下,效果很好

<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>

$(文档).ready(函数(){ AddDropdownItem(“-选择某个测试-”、“-1”、“testDropdown”); AddDropdownItem(“其他东西”、“1”、“testDropdown”); AddDropdownItem(“另一件事”,2,“testDropdown”); 变量id=104388426283811; AddDropdownItem(“测试大值”,id.toString(),“testDropdown”); AddDropdownItem(“个人资料图片”,10000379631246,“测试下拉”); AddDropdownItem(“TestTest2”,104388426283811,“testDropdown”); }); 函数AddDropdownItem(sText、sValue、sDropdownID) { $(“#”+sDropdownID).append(“+sText+”); }

您的原始代码实际上也可以工作。我不认为这有什么问题。:)可能代码中的其他地方有问题?

JavaScript将在需要时将整数解释为字符串,无需使用
toString()

根据原始问题中的注释线程,以下是我的示例页面,其中包含了您的逻辑(以及连接一个按钮以向选择添加选项):


jQuery测试
$(文档).ready(函数(){
$(“#b单击”)。单击(函数(e){
e、 预防默认值();
变量id=104388426283811;
AddDropdownItem(“-Select something test-”,id.toString(),“testDropdown”);
//AddDropdownItem(“-选择某个测试-”、“-1”、“testDropdown”);
});
});
函数AddDropdownItem(sText、sValue、sDropdownID)
{
$(“#”+sDropdownID).append(“+sText+”);
}

我没有您提到的iFrame,但我正在使用
input submit
元素添加项目。我通过调用
e.preventDefault()来防止提交按钮的默认行为,这将阻止回发。然后,我可以将项目添加到
select
元素中


我希望这有帮助。

id是一种基本类型吗?它被设置为整数这没有什么区别,因为id是int.toString()或no,它工作得很好。我没有收到任何错误——选项加载得很好。我在IE8和FF3.6中这样做了。你能发布更多的代码吗?我不确定你指的是什么默认选项(是第一个添加的选项吗)?也许这就是问题的原因?好吧,这段代码正在工作(没有我以前尝试过的toString),但不喜欢我尝试添加的最后一个选项。下面是add方法的传入参数:sText:“Profile Pictures”sValue=10000379631246 sDropdownID=“testDropdown”,这是第一个(在默认选项之后的第二个)看起来很好并成功添加的参数:sText=“Test Test2”sValue=104388426283811 sDropdownID=“testDropdown”我不明白为什么它添加的后一个选项会有问题,这是该配置文件图片值的选项3。我正在尝试它,并且没有任何错误(Chrome、FF、IE)。因此,这将消除或容忍任何空间?加上单引号?它可以容纳空格。如果你习惯于使用引号(单引号或双引号),那么你将来就不会遇到那么多问题了。所以在DOM加载后,它会将什么作为e发送给你?DOM作为一个对象?我看到你也在使用提交,我的是一个imageIt是处理事件的元素(事件对象)。在这种情况下,单击submit按钮。下面是这个对象的jQueryAPI条目以及与之关联的函数/道具<代码>
是一个带有图像的提交按钮。同样的事情——当您单击表单时,它应该尝试提交表单。使用
preventDefault
来阻止它这样做,你应该做得很好。太棒了!很高兴你让它工作了。这是很多评论。:)稍后。
function AddDropdownItem(sText, sValue, sDropdownID)
{
      $("#" + sDropdownID).append("<option value='" + sValue + "'>" + sText + "</option>");
}
<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>
<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>