jQuery:获取HTML以及输入值 [英] jQuery: Get HTML as well as input values

查看:78
本文介绍了jQuery:获取HTML以及输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个变量将HTML存储在div标签中,但只需使用 var a = $('div')。html() doesn' t存储位于div内的输入标签的值。



所以,我的问题是,我应该如何将使用jQuery的HTML 选定选项和输入标记的值保存到变量?



以下是一些示例代码:

HTML:

 < div> 
< p>一些文字< / p>
< select name =word>
< option value =1>占位符1< / option>
< option value =2>占位符2< / option>
< / select>
< input type =text/>
< / div>

Javascript:

  / *a也应该有用户值,这样当我使用$('body')。append(a),
时,它具有与div相同的用户输入。 * /

var a = $('div')。html();

预先致谢。

解决方案

您可以 $。clone()

  var $ a = $(div:first)。clone(); 

$ a.appendTo(body); // Clone侵入你的身体

在线演示: http://jsbin.com/obebov/edit


I'm trying to have a variable store the HTML in a div tag, but simply using var a = $('div').html() doesn't store the values of the input tags that lie within the div.

So, my question is, how should I go about saving the HTML and the selected options and values of input tags to a variable using jQuery?

Here is some example code:

HTML:

<div>
  <p>Some Text</p>
  <select name="word">
    <option value="1">Placeholder 1</option>
    <option value="2">Placeholder 2</option>
  </select>
  <input type="text" />
</div>

Javascript:

/* "a" should also have the user values, such that when I use $('body').append(a), 
it has the same user input as the div. */

var a = $('div').html(); 

Thanks in advance.

解决方案

You could $.clone() the element.

var $a = $("div:first").clone();

$a.appendTo("body"); // Clone invades your body

Online Demo: http://jsbin.com/obebov/edit

这篇关于jQuery:获取HTML以及输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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