最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting text box value to int - Stack Overflow

programmeradmin5浏览0评论

I have been trying to apply solutions I found on both Google and Stackoverflow, but they don't seem to be working.

What exactly is going wrong here? The checkbox is insignificant here and I can take it out, but it makes no difference.

<div class="form-group">
    <label for="tbid" class="col-lg-3 control-label">Results per page </label>
        <div class="col-lg-3">
            <input type="checkbox" class="checkbox" display="inline-block" name="checkbox_results" onclick="checkbox_results_click();">
            <input type="text" id="results" class="form-control" name="results" placeholder="results">
        </div>
</div>

Then in the js portion, I am trying to convert results into an int.

<script type="text/javascript">
    int results = null;
    var x=document.getElementById("results").value;

    results = parseInt(x);
    if(results==null)
        results=10;

    var pager = new Pager(results);

</script>

EDIT: I should also add that if I just put a int parameter when calling pager, like 25, for example, it actually works. So something is going wrong with results.

I have been trying to apply solutions I found on both Google and Stackoverflow, but they don't seem to be working.

What exactly is going wrong here? The checkbox is insignificant here and I can take it out, but it makes no difference.

<div class="form-group">
    <label for="tbid" class="col-lg-3 control-label">Results per page </label>
        <div class="col-lg-3">
            <input type="checkbox" class="checkbox" display="inline-block" name="checkbox_results" onclick="checkbox_results_click();">
            <input type="text" id="results" class="form-control" name="results" placeholder="results">
        </div>
</div>

Then in the js portion, I am trying to convert results into an int.

<script type="text/javascript">
    int results = null;
    var x=document.getElementById("results").value;

    results = parseInt(x);
    if(results==null)
        results=10;

    var pager = new Pager(results);

</script>

EDIT: I should also add that if I just put a int parameter when calling pager, like 25, for example, it actually works. So something is going wrong with results.

Share Improve this question edited Oct 25, 2013 at 3:56 krikara asked Oct 25, 2013 at 3:10 krikarakrikara 2,44510 gold badges39 silver badges75 bronze badges 4
  • Note that parseInt always returns either a number or NaN, neither of which ever == null. So the test should be if (!isNaN(results)). Also, parseInt should be used with a radix parseInt(x,10) – RobG Commented Oct 25, 2013 at 3:50
  • Yeah I did x,10 at first, but it didn't seem to make a difference. For whatever reason, it wasn't getting into the pager. I'll change it back now – krikara Commented Oct 25, 2013 at 3:53
  • The radix won't fix your issue, it just means results will be set to the value you expect. parseInt('09') returns 9 in some browsers, 1 in others whereas parseInt('09',10) returns 9 everywhere. – RobG Commented Oct 25, 2013 at 3:57
  • Possible duplicate of convert string to a number in javascript – Liam Commented Jul 15, 2016 at 14:25
Add a ment  | 

4 Answers 4

Reset to default 2

Instead of using parseInt use parseFloat it will work like this

results = parseFloat(x);

there's no int in js, you could do:

var results;
var x=document.getElementById("results").value;

results = /^[\d]+$/.test(x);
if(!results)
    results=10;

var pager = new Pager(results);

The trick is +(element.value), add || default_value in case is invalid, or nothing. Or add your own checks.

<!DOCTYPE html>
<html><head><title>to int()</title>
<script type="text/javascript">
function doResults()
{
    var element = document.getElementById('results'),
        value = +(element.value) || 0;

    // write back the value in case is invalid
    element.value = value;

    alert("The value is " + value);
}
</script></head>
<body>
<input type="text" id="results" class="form-control" name="results" placeholder="results">
<button onclick="doResults();">Check Results</button>
</body></html>

Use parseFloat it will work like this

<script type="text/javascript">
    int results = null;
    var x=document.getElementById("results").value;

    results = parseFloat(x);
    if(results==null)
        results=10;

    var pager = new Pager(results);

</script>

发布评论

评论列表(0)

  1. 暂无评论