te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>dom - How to assign a javascript variable to input text field? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - How to assign a javascript variable to input text field? - Stack Overflow

programmeradmin4浏览0评论

I've a variable called "numbers" in javascript and this variables holds 10 numbers from 0 to 9 as shown below.

var numbers = "0123456789";

Now what I want to be able to do is assigning each of these numbers to an input text field using document.getElementByID("a") = "";, for example:

<input type="text" id="a" value="" />
<input type="text" id="b" value="" />
<input type="text" id="c" value="" />
<input type="text" id="d" value="" />
<input type="text" id="e" value="" />
<input type="text" id="f" value="" />
<input type="text" id="g" value="" />
<input type="text" id="h" value="" />
<input type="text" id="i" value="" />
<input type="text" id="j" value="" />

Currently the following text fields above holding no values, but I want to be able to assign each of the numbers in variable "numbers" to each of the text fields above, so it would looks like this when user clicks on a button called click me.

<input type="text" id="a" value="0" />
<input type="text" id="b" value="1" />
<input type="text" id="c" value="2" />
<input type="text" id="d" value="3" />
<input type="text" id="e" value="4" />
<input type="text" id="f" value="5" />
<input type="text" id="g" value="6" />
<input type="text" id="h" value="7" />
<input type="text" id="i" value="8" />
<input type="text" id="j" value="9" />

Also is there any way to leave a text field empty if a particular number is = 0

Thanks in advance :)

I've a variable called "numbers" in javascript and this variables holds 10 numbers from 0 to 9 as shown below.

var numbers = "0123456789";

Now what I want to be able to do is assigning each of these numbers to an input text field using document.getElementByID("a") = "";, for example:

<input type="text" id="a" value="" />
<input type="text" id="b" value="" />
<input type="text" id="c" value="" />
<input type="text" id="d" value="" />
<input type="text" id="e" value="" />
<input type="text" id="f" value="" />
<input type="text" id="g" value="" />
<input type="text" id="h" value="" />
<input type="text" id="i" value="" />
<input type="text" id="j" value="" />

Currently the following text fields above holding no values, but I want to be able to assign each of the numbers in variable "numbers" to each of the text fields above, so it would looks like this when user clicks on a button called click me.

<input type="text" id="a" value="0" />
<input type="text" id="b" value="1" />
<input type="text" id="c" value="2" />
<input type="text" id="d" value="3" />
<input type="text" id="e" value="4" />
<input type="text" id="f" value="5" />
<input type="text" id="g" value="6" />
<input type="text" id="h" value="7" />
<input type="text" id="i" value="8" />
<input type="text" id="j" value="9" />

Also is there any way to leave a text field empty if a particular number is = 0

Thanks in advance :)

Share Improve this question edited May 13, 2011 at 15:12 mellamokb 56.8k12 gold badges110 silver badges138 bronze badges asked May 13, 2011 at 15:10 ZADZAD 5692 gold badges7 silver badges23 bronze badges 1
  • 2 You can get one number of the String using numbers[2] (to get the third character which is two), so something like if(numbers[2] != 0) document.getElementById('c').value = numbers[2] you might want to play with. – pimvdb Commented May 13, 2011 at 15:14
Add a ment  | 

3 Answers 3

Reset to default 8
var numbers = "0123456789";
var ids = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];

for (var i = 0; i < ids.length; i++)
{
    var el = document.getElementById(ids[i]);
    var num = numbers[i];

    if (num == "0")
        el.value = "";
    else
        el.value = num;
}

Working sample: http://jsfiddle/LrJ5S/.

In addition to mellamokb, another way of acplishing the same:

var numbers = "abc0123abc0123";
var inputs = document.getElementsByTagName('input');
var i = 0;
for(var s = 0; s < inputs.length; s++) {
    inputs[s].value = numbers.charAt(i)!=0?numbers.charAt(i):'';
    i++;
}

JSFiddle

Basic example with no checks on data size of inputs and string:

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title></title>
  </head>
  <body>
    <div id="myInputs">
      <input type="text" id="a" value="" />
      <input type="text" id="b" value="" />
      <input type="text" id="c" value="" />
      <input type="text" id="d" value="" />
      <input type="text" id="e" value="" />
      <input type="text" id="f" value="" />
      <input type="text" id="g" value="" />
      <input type="text" id="h" value="" />
      <input type="text" id="i" value="" />
      <input type="text" id="j" value="" />      
    </div>
    <button id="run">Run</button>

    <script type="text/javascript">
        function putNumbers(){
           var numbers = "0123456789";
           var inputs = document.getElementById("myInputs").getElementsByTagName("input");
           for(var i=0;i<inputs.length;i++){
             var val = numbers.charAt(i);
             inputs[i].value = val==="0"?"":val;
           }  

        }

        document.getElementById("run").onclick = putNumbers;
    </script>


  </body>
</html>

working Example

发布评论

评论列表(0)

  1. 暂无评论