Javascript设置输入字段的值

时间:2013-04-19 09:58:04

标签: javascript html5 input setattribute

因为我无法使用type = text设置输入字段的值。 之前,我总是使用这样的东西:

<input style="display: none" type="text" name="geo_poi" value="" id="geofeld" />

然后,在JavaScript中我添加了包含如下行的代码:

document.getElementById("geofeld").value = geo_poi;

这总是奏效的。也许新的浏览器不再支持上面的方法了。

3 个答案:

答案 0 :(得分:16)

因此,使用以下方法设置属性工作正常。

document.getElementById("geofeld").setAttribute("value", geo_poi);

答案 1 :(得分:1)

对于setAttribute不产生任何结果(想想HTML5用户/密码输入)的情况,请考虑以下情况:

class mail_thread(osv.AbstractModel):
    _inherit = 'mail.thread'    

    def _get_followers(self, cr, uid, ids, name, arg, context=None):
        fol_obj = self.pool.get('mail.followers')
        fol_ids = fol_obj.search(cr, SUPERUSER_ID, [('res_model', '=', self._name), ('res_id', 'in', ids)])
        res = dict((id, dict(message_follower_ids=[], message_is_follower=False)) for id in ids)
        user_pid = self.pool.get('res.users').read(cr, uid, [uid], ['partner_id'], context=context)[0]['partner_id'][0]
        for fol in fol_obj.browse(cr, SUPERUSER_ID, fol_ids):
            res[fol.res_id]['message_follower_ids'].append(fol.partner_id.id)
            if fol.partner_id.id == user_pid:
                res[fol.res_id]['message_is_follower'] = True
        return res

    def _search_followers(self, cr, uid, obj, name, args, context):
        """Search function for message_follower_ids

        Do not use with operator 'not in'. Use instead message_is_followers
        """
        fol_obj = self.pool.get('mail.followers')
        res = []
        for field, operator, value in args:
            assert field == name
            # TOFIX make it work with not in
            assert operator != "not in", "Do not search message_follower_ids with 'not in'"
            fol_ids = fol_obj.search(cr, SUPERUSER_ID, [('res_model', '=', self._name), ('partner_id', operator, value)])
            if not fol_ids and operator == '=' and value==False:
                fol_ids = fol_obj.search(cr, SUPERUSER_ID, [('res_model', '=', self._name), ('partner_id', '!=', value)])
                res_ids = [fol.res_id for fol in fol_obj.browse(cr, SUPERUSER_ID, fol_ids)]
                res.append(('id', 'not in', res_ids))
            else:
                res_ids = [fol.res_id for fol in fol_obj.browse(cr, SUPERUSER_ID, fol_ids)]
                res.append(('id', 'in', res_ids))
        return res

    _columns = {
        'message_follower_ids': fields.function(_get_followers,fnct_search=_search_followers),
    }

答案 2 :(得分:-2)

HTML


<input type="text" placeholder="Item 1" id="item1" />
<input type="text" placeholder="Item 2" id="item2" />



JAVASCRIPT
$('#item1').blur(function() {
    var item1var = $('#item1').val();
    $('#item2').val(item1var);
});

Jsfiddle HERE