我发现jQuery没有收到表单的值有问题。
我将表单隐藏在div中,用户点击“发送下午”即可显示该表单。
<div class="interactContainers" id="private_message1">
<form action="javascript:sendPM();" name="pmForm" id="pmForm" method="post">
<font size="+1">Sending Private Message to <strong><em><?php echo "$username"; ?></em></strong></font><br /><br />
Subject:
<input name="pmSubject" id="pmSubject" type="text" maxlength="64" style="width:90%;" />
Message:
<textarea name="pmTextArea" id="pmTextArea" rows="8" style="width:90%;"></textarea>
<input name="pm_sender_id" id="pm_sender_id" type="hidden" value="<?php echo $sessionid ?>" />
<input name="pm_sender_name" id="pm_sender_name" type="hidden" value="<?php echo $user ?>" />
<input name="pm_rec_id" id="pm_rec_id" type="hidden" value="<?php echo $profileid ?>" />
<input name="pm_rec_name" id="pm_rec_name" type="hidden" value="<?php echo $username ?>" />
<input name="pmWipit" id="pmWipit" type="hidden" value="<?php echo $thisRandNum ?>" />
<span id="PMStatus" style="color:#F00;"></span>
<br /><input name="pmSubmit" type="submit" value="Submit" />
<span id="pmFormProcessGif" style="display:none;"><img src="../_Images/loading.gif" width="28" height="10" alt="Loading" /></span></form>
</div>
我有一个JQuery脚本,用于检查信息是否已输入,但它一直告诉我数据未被接收。
我不断获得“请输入主题”或“请输入消息”输出
JQuery
$('#pmForm').submit(function(){$('input[type=submit]', this).attr('disabled', 'disabled');});
function sendPM ( ) {
var pmSubject = $("#pmSubject");
var pmTextArea = $("#pmTextArea");
var sendername = $("#pm_sender_name");
var senderid = $("#pm_sender_id");
var recName = $("#pm_rec_name");
var recID = $("#pm_rec_id");
var pm_wipit = $("#pmWipit");
var url = "../_Scripts/private_msg_parse.php";
if (pmSubject.val() == "") {
$("#jqueryReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> Please type a subject.').show().fadeOut(6000);
} else if (pmTextArea.val() == "") {
$("#jqueryReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> Please type in your message.').show().fadeOut(6000);
} else {
$("#pmFormProcessGif").show();
$.post(url,{ subject: pmSubject.val(), message: pmTextArea.val(), senderName: sendername.val(), senderID: senderid.val(), rcpntName: recName.val(), rcpntID: recID.val(), thisWipit: pm_wipit.val() } , function(data) {
$("#jqueryReply").html(data).show().fadeOut(10000);
document.pmForm.pmTextArea.value='';
document.pmForm.pmSubject.value='';
$("#pmFormProcessGif").hide();
});
}
}
这是一个小提琴,看看发生了什么。 http://jsfiddle.net/RKN39/
答案 0 :(得分:0)
如果您将表单的值解析为Jquery中的变量,则必须告诉jQuery您需要什么值。使用.val(),jQuery会选择文本字段的值。
var pmSubject = $("#pmSubject").val();
var pmTextArea = $("#pmTextArea").val();
var sendername = $("#pm_sender_name").val();
var senderid = $("#pm_sender_id").val();
var recName = $("#pm_rec_name").val();
var recID = $("#pm_rec_id").val();
var pm_wipit = $("#pmWipit").val();
希望它有效!
答案 1 :(得分:0)
为什么不序列化表单?
$( '#pmForm').on( "submit", function( event ) {
event.preventDefault();
var form_data = $( this ).serialize() ;
console.log(form_data);
var pmSubject = $("#pmSubject");
var pmTextArea = $("#pmTextArea");
var url = "../_Scripts/private_msg_parse.php";
if (pmSubject.val() === ""){
$("#jqueryReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> Please type a subject.').show().fadeOut(6000);
} else if (pmTextArea.val() === "") {
$("#jqueryReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> Please type in your message.').show().fadeOut(6000);
} else {
$("#pmFormProcessGif").show();
$.post(url, form_data, function(data) {
$("#jqueryReply").html(data).show().fadeOut(10000);
pmTextArea.val("");
pmSubject.val("");
$("#pmFormProcessGif").hide();
});
}
});