使用 javascript 设置选择下拉列表中的值

发布于 2024-12-02 10:19:33 字数 1418 浏览 1 评论 0原文

我在尝试在选择中设置值时遇到问题。我一直看到 NaN 作为我的结果(在某些浏览器中 - 主要是 Android 智能手机/平板电脑默认浏览器)

条件为 true 时设置值:

if (variable1 >= variable2)
{
    $('select[title="title"]').val(variable1);
}

我正在运行一个简单的 if 语句并在 if 语句将它们都显示为数字。尽管如此,浏览器中 select 中显示的结果值为 NaN。

我什至尝试将 javascript Number() 函数包装在变量周围,如下所示:

if (Number(variable1) >= Number(variable2))
{
    $('select[title="myTitle"]').val(Number(variable1));
}

但我仍然得到相同的结果!

注意:这在台式机/笔记本电脑浏览器甚至我的三星 Galaxy S2 上的 Opera Mobile 中运行良好,但由于我正在努力将其打造为一个可以在任何手机上运行的应用程序,所以我希望完成错误检查。

编辑: 如果这是我的选择,那就是问题(如下所示)这里有什么问题? 是因为它在我返回时被转换为字符串吗?

var startPoint = this.do_calcTotal(); // typeof startPoint shows as number
var endPoint = startPoint + 12; // typeof endPoint shows as number

var mySelect =   '<select title="myTitle" class="myClass">';

                    for (o=startPoint; o<endPoint; o++)
                    {
                        mySelect += '<option>'+Number(o)+'</option>'; // is this right?? it shows 'o' and 'Number(o)' as typeof number!?
                    }

mySelect += '</select></span></div>';

return mySelect;

编辑:以及将当前选定值添加到范围以进行显示的代码...

function do_changeNumberSpan(value)
{
    $('.mySpan').text(Number(value)); // even tried throwing a Number() round the value here!!
}

I am having trouble when trying to set a value in a select. I keep seeing NaN as my result (in some browsers - mainly android smartphone / tablet default browsers)

I am running a simply if statement and setting the value if the condition is true:

if (variable1 >= variable2)
{
    $('select[title="title"]').val(variable1);
}

Where a 'typeof' check on both variable1 and variable2 before the if statement show them both as numbers. Despite this the resulting value shown in select in the browser is NaN.

I've even tried wrapping the javascript Number() function around the variables like so:

if (Number(variable1) >= Number(variable2))
{
    $('select[title="myTitle"]').val(Number(variable1));
}

But I still get the same result!

Note: This works fine in desktop / laptop browsers and even Opera Mobile on my Samsung Galaxy S2 but as I'm working towards this being an app to run on any phone I wanted to be complete in my error checking.

edit:
In case it's my select thats the problem (as suggested below) what's wrong with it here?
Is it because it is converted to a string at the point I return it?

var startPoint = this.do_calcTotal(); // typeof startPoint shows as number
var endPoint = startPoint + 12; // typeof endPoint shows as number

var mySelect =   '<select title="myTitle" class="myClass">';

                    for (o=startPoint; o<endPoint; o++)
                    {
                        mySelect += '<option>'+Number(o)+'</option>'; // is this right?? it shows 'o' and 'Number(o)' as typeof number!?
                    }

mySelect += '</select></span></div>';

return mySelect;

Edit: and the code to add the current selected value to the span for display...

function do_changeNumberSpan(value)
{
    $('.mySpan').text(Number(value)); // even tried throwing a Number() round the value here!!
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2024-12-09 10:19:33

好的。看看这个jsfiddle。这应该可以帮助您了解一些您的目标是什么......以及如何实现它。
http://jsfiddle.net/aaronfrost/yFuyu/

Ok. Check out this jsfiddle. This should help you understand a little bit of what your goal is... and how you can accomplish it.
http://jsfiddle.net/aaronfrost/yFuyu/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文