将HTMLString(带有输入类型文本)解析为String。

时间:2017-04-28 17:11:24

标签: javascript jquery string

我真的需要你的帮助。我有这个表单与文本和输入字段。

enter image description here

我做了一点点jquery并得到了大的htmlString:

var StringContent = '<p>I am thrilled to share my new listing with you at <input id="additions_info_0_0" name="additions[0][replace][0]" size="21" type="text" value="Property Address"><input name="additions[0][find][0]" type="hidden" value="(MUST TYPE Property Address HERE)">. To find out more about this exciting property, or to hear about one of the many others currently on the market, call me to set up an appointment at <input id="additions_info_0_1" name="additions[0][replace][1]" size="17" type="text" value="Phone Number"><input name="additions[0][find][1]" type="hidden" value="(MUST TYPE Phone Number HERE)">. I would be happy to answer your questions, discuss your needs, and set you on track to achieving your real estate goals. Looking forward to hearing from you soon!</p>';

我可以遍历htmlString并获取输入值,例如&#34; 1003 Audelia,Houston,TX 75002&#34;输入id = additions_info_0_0和&#34; 469-999-9999&#34;对于id additions_info_0_1。但我无法将所有字符串和输入值组合成新的字符串,例如我很高兴与您分享我的新商品 1003 Audelia,Houston,TX 75002 。要了解有关这个令人兴奋的房产的更多信息,或了解目前市场上众多其他房产中的一个,请致电我在 469-999-9999 预约。我很乐意回答您的问题,讨论您的需求,并让您按计划实现您的房地产目标。期待很快收到您的回复!

我试图解析HTML但没有运气。任何帮助都会很棒,非常感激。

1 个答案:

答案 0 :(得分:2)

您的问题不是很明确我希望此代码能够满足您的要求。 以类似的方式更改HTML,

var n = str.indexOf("[]");

和你的javascript代码一样

<div id="contentDiv">
  <p>I am thrilled to share my new listing with you at
    <input id="additions_info_0_0" name="additions[0][replace][0]" size="21" type="text" value="Property Address">. To find out more about this exciting property, or to hear about one of the many others currently on the market, call me to set up an appointment at
    <input id="additions_info_0_1" name="additions[0][replace][1]" size="17" type="text" value="Phone Number"> I would be happy to answer your questions, discuss your needs, and set you on track to achieving your real estate goals. Looking forward to hearing from you soon!</p>
</div>
<input type="button" id="btnSubmit" value="Submit">

这段代码将首先显示来自HTML的内容,输入值后,您可以单击按钮,该按钮将触发javascript代码并从文本框中取出两个输入值并重新格式化要显示的文本。然后,您可以根据需要将div中的初始内容更改为格式化内容。

我已经使用JQuery完成了它,因为您还没有提到您希望代码的用途。