权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - Plus operator problems in Jquery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Plus operator problems in Jquery - Stack Overflow

programmeradmin10浏览0评论

I was trying with following script

<script src=".7.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#item1_number_1').keyup(function() {
            var valone = $('#item1_number_1').val();
            var valtwo = 5;
            var total = ((valone) + (valtwo));
            $('#item2_number_1').val(total.toFixed(2));
        });
    });
</script>

I do not get any result in the field. But when I assign multiple (*) instead of plus (+), I am getting result.

I cannot understand what the error is in "var total = ((valone) + (valtwo));"

I was trying with following script

<script src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#item1_number_1').keyup(function() {
            var valone = $('#item1_number_1').val();
            var valtwo = 5;
            var total = ((valone) + (valtwo));
            $('#item2_number_1').val(total.toFixed(2));
        });
    });
</script>

I do not get any result in the field. But when I assign multiple (*) instead of plus (+), I am getting result.

I cannot understand what the error is in "var total = ((valone) + (valtwo));"

Share Improve this question edited Mar 4, 2014 at 13:39 Tushar Gupta - curioustushar 57.1k24 gold badges105 silver badges109 bronze badges asked Mar 4, 2014 at 13:39 Mehedee Rahman SetuMehedee Rahman Setu 1074 silver badges13 bronze badges 1
  • Can you make a fiddle of this please – BenM Commented Mar 4, 2014 at 13:41
Add a ment  | 

5 Answers 5

Reset to default 4

You can only call toFixed on Numbers.

String * String will convert the strings to Numbers and multiply them giving you a Number.

String + String will concatenate the two Strings together giving you a String.

You need to convert the strings to Numbers manually before you try to add them together.

var total = (+valone) + (+valtwo);

Then Number + Number will add the two Numbers together giving you a Number.

The value of an input is always a string. "Adding" a string concatenates, giving another string. Strings do not have a toFixed method.

* however is unambiguously "multiply", giving a number and therefore a result.

var valone = parseFloat(document.getElementById('item1_number_1').value);

Use parseInt() to convert fetched value(valone ) to number, and calculate, something like this, please use this only when your number is not float(56.66),

var valone =  parseInt($('#item1_number_1').val(), 10);
var valtwo = 5;
var total = ((valone) + (valtwo));

The fetched vaue is treated like string until you convert it into number.

UPDATE

After Archer pointed out, I came to know you are using toFixed() method, which supposed to expect float numbers. So in this case you should use parseFloat() as given below.

 var valone = parseFloat($('#item1_number_1').val());

I think one of them is a string. Try parseInt(valone) to make it an int first.

The issue is the + operator can also be used to concat strings together. The * operator is ONLY for multiplication and therefore it implicitly converts your values to numbers.

So you either need to use parseInt, parseFloat, or Number to explicitly convert to a numeric type before using the + operator.

发布评论

评论列表(0)

  1. 暂无评论