我有一张表格
<form id="post_comment" action="cmt.php" method="post">
<input type="hidden" name="type" value="sub" />
<textarea id="body"></textarea>
</form>
我正在使用此代码访问表单
$("#post_comment").submit(function(event){
var form = $(this);
});
如何从此表单中获取<input type="hidden" name="type" value="sub" />
的值。
我试图使用form.input("type")
,但它无效。
答案 0 :(得分:5)
$("#post_comment").submit(function(event){
var inputValue = $("input[name='type']",this).val();
});
答案 1 :(得分:4)
尝试使用这样的ID:
<form id="post_comment" action="cmt.php" method="post">
<input type="hidden" id='hidden' name="type" value="sub" />
<textarea id="body"></textarea>
</form>
以后:
$("#post_comment").submit(function(event){
var hiddenValue = $("#hidden").val();
});
答案 2 :(得分:2)
var form = $(this);
var inputValue = form.find('input[name="type"]').val();
or
var form = $(this);
var inputValue = form.find('input:hidden').val();
答案 3 :(得分:2)
<form id="post_comment" action="" method="post">
<input type="hidden" class="hidden" name="type" value="sub" />
<textarea id="body"></textarea>
<input type="submit" value="submit" class="submit"/>
</form>
$(".submit").click(function(){
var hiddenVal=jQuery("#post_comment .hidden").val();
//alert(hiddenVal);
});
答案 4 :(得分:1)
另一种方法
考虑一下,如果您具有多个表单,并且多个输入字段具有名称属性,则此代码对您有帮助:
<?php
$username = 'username';
$password = 'password';
$url = "your url";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch,CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, 1);
$first_response = curl_exec($ch);
$info = curl_getinfo($ch);
preg_match('/WWW-Authenticate: Digest (.*)/', $first_response, $matches);
if(!empty($matches))
{
$auth_header = $matches[1];
$auth_header_array = explode(',', $auth_header);
$parsed = array();
foreach ($auth_header_array as $pair)
{
$vals = explode('=', $pair);
$parsed[trim($vals[0])] = trim($vals[1], '" ');
}
$response_realm = (isset($parsed['realm'])) ? $parsed['realm'] : "";
$response_nonce = (isset($parsed['nonce'])) ? $parsed['nonce'] : "";
$response_opaque = (isset($parsed['opaque'])) ? $parsed['opaque'] : "";
$authenticate1 = md5($username.":".$response_realm.":".$password);
$authenticate2 = md5("POST:".$url);
$authenticate_response = md5($authenticate1.":".$response_nonce.":".$authenticate2);
$request = sprintf('Authorization: Digest username="%s", realm="%s", nonce="%s", opaque="%s", uri="%s", response="%s"',
$username, $response_realm, $response_nonce, $response_opaque, $url, $authenticate_response);
$request_header = array($request);
$request_header[] = 'Content-Type:application/json';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch,CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_header);
$result['response'] = curl_exec($ch);
$result['info'] = curl_getinfo ($ch);
$result['info']['errno'] = curl_errno($ch);
$result['info']['errmsg'] = curl_error($ch);
var_dump($result);
}
?>
希望对别人有帮助。
答案 5 :(得分:0)
$("#post_comment").submit(function(event){
var form = $("input[name='type']").val();
})