简体   繁体   English

使用jquery在原始html中获取html表单输入值

[英]using jquery to get html form input values in raw html

How do you use jquery (or anything else for that matter) to get the form values from the argument in the callback of a jquery.get() call provided it is html? 你如何使用jquery(或其他任何东西)从jquery.get()调用的回调中的参数中获取表单值,前提是它是html?

for instance, say the following call: 例如,请说以下电话:

$.get('somePage.aspx', function (data) {  
     alert('here');
            });

called the callback function with the following html: 用以下html调用回调函数:

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
    <form name="form1" method="post" action="frmMontageWait.aspx?action=checkStatus&amp;guid=a224b7c3-fec8-4b55-870e-a33f15bad629" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTc5NTA2NTY5NmRkyx3R93TAvDqSvxEh6aKHeTSr0ZI=" />
</div><div> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwLA8/fiDgLP15SfBAKG69WwDBRTCbRBksmbw/qTkRQ4tx/K3bES" />
</div>
        <input type="hidden" name="hdnInput1" id="hdnInput1" value="100" />
        <input type="hidden" name="hdnInput2" id="hdnInput2" value="99" />
    </form>
</body>
</html>

How do you go about getting the values of hdnInput1 and hdnInput2 in the callback? 你如何在回调中获取hdnInput1和hdnInput2的值?

The data variable in your code stores the server-response, you can parse it for the information you want. 代码中的data变量存储服务器响应,您可以解析它以获取所需的信息。 By default this server response will be in plain text, so you have to parse that string into a jQuery object which will create DOM elements out of the string: 默认情况下,此服务器响应将以纯文本形式显示,因此您必须将该字符串解析为jQuery对象,该对象将从字符串中创建DOM元素:

//DOM-ize the server-response
data = $(data);

//now we can search the server-response like it is in the DOM (but it isn't)
var inputOne = data.find('#hdnInput1').val(),
    inputTwo = data.find('#hdnInput2').val();

This code goes in your success callback for your AJAX call. 这段代码将成为您AJAX调用的success回调。

Here is a demo: http://jsfiddle.net/PDHbV/ 这是一个演示: http//jsfiddle.net/PDHbV/

I'm not 100% sure that the HTML you posted is stored in the data variable, if it isn't then that mean the HTML is already in the DOM and you can search for it normally: 我不是100%确定您发布的HTML存储在data变量中,如果不是那么意味着HTML已经存在于DOM中,您可以正常搜索它:

var inputOne = $('#hdnInput1').val(),
    inputTwo = $('#hdnInput2').val();
val1 = $('#hdnInput1').val();
val2 = $('#hdnInput2').val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM