为什么jQuery设置属性未反映在.html()中? [英] Why are jQuery set attributes not reflected in .html()?

查看:70
本文介绍了为什么jQuery设置属性未反映在.html()中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在HTML代码段上设置一些属性.目的是用先前输入的值重新填充表单.我使用.attr()设置属性,但是执行.html()后,看不到更改后的属性.

I am trying to set some attributes on HTML snippets. The purpose is to repopulate a form with previous inputed values. I set attributes with .attr() but after I do a .html(), I do not see my changed attributes.

我在这里做了一个基本示例 http://jsfiddle.net/ejanderson4/CSYnU/

I have done a basic example here http://jsfiddle.net/ejanderson4/CSYnU/

我的功能如下:

function setValue(html,value,name){
    var element=$(html);
    $(element).find("[name='"+name+"']").attr('value',value)
    return element.html();
}

推荐答案

正在设置它,但是由于我不知道的原因,您不能直接查询输入元素的value属性.您需要在该元素上调用.val()来获取它.

It is setting it, but for reasons unknown to me, you can't directly query the value attribute of an input element. You need to call .val() on that element to get it.

更新了示例此处

这篇关于为什么jQuery设置属性未反映在.html()中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆