我是Jquery的初学者,我试着做一些简单的例子来练习,
在本例中,我试图将POST var从php表单发送到我在internet上找到的Jquery modale弹出窗口。
我的问题是,我无法将表单变量发送到modale弹出窗口,当我单击submit链接发送php表单时,
弹出窗口在我的案例$name中不带var。
这里是我的代码:
Index.php:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="test.css" />
<script type="text/javascript" src="script.js"></script>
<form method = "POST" id = "formm" name="form1" action="recep.php" >
<input type="text" name="nom"/> <br>
<a href="#?w=500" rel="popup_name" class="poplight" name="nom" onclick="openwindow(this)";">test</a><br>
</form>
最后我的script.js:
$(document).ready(function() {
$('a.poplight[href^=#]').click(function() {
var popID = $(this).attr('rel'); //Trouver la pop-up correspondante
var popURL = $(this).attr('href'); //Retrouver la largeur dans le href
//Récupérer les variables depuis le lien
var query= popURL.split('?');
var dim= query[1].split('&');
var popWidth = dim[0].split('=')[1]; //La première valeur du lien
$('#' + popID).fadeIn().css({
'width': Number(popWidth)
})
.prepend('');
var popMargTop = ($('#' + popID).height() + 80) / 2;
var popMargLeft = ($('#' + popID).width() + 80) / 2;
$('#' + popID).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
$('body').append('');
//Apparition du fond - .css({'filter' : 'alpha(opacity=80)'})
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
return false;
});
$('a.close, #fade').live('click', function() { //Au clic sur le bouton ou sur le calque...
$('#fade , .popup_block').fadeOut(function() {
$('#fade, a.close').remove();
});
return false;
});
});
});
recep.php
<div id="popup_name" class="popup_block">
<h2>Developpez.com</h2>
<p>Soh Tanaka est traduit sur dev.com.<?php if (isset($_POST['nom'])) { $nom = $_POST['nom']; echo 'la var est'.$nom; } ?></p>
</div>
我想我必须将submit()条件添加到script.js中,因为当我单击链接Jquery时,只选择了href,但他没有提交表单。
知道吗?? :(
谢谢您!!!
最佳答案
看起来试图发送表单但您没有提交它,并且您试图在recep.php上获取的值位于无法发送的锚上!此外,input和anchor都有name=“nom”。单击锚定时还调用了两个函数。所以有很多事情你想分开。
首先,您应该决定是使用jQuery、javascript还是同时使用两者来调用所需的函数。
如果您使用的是Jquery,那么在页面加载时需要一种方法来调用元素
$('.whateverClass').click(function() { etc });
在这种情况下,我将使用一个类或id属性。
如果您使用onclick,您将调用一个javascript函数。在这种情况下,您应该将所需的所有参数作为数组传递
onclick="callTheFunction(this , '{put an array here with what you want?}')"
所以这个锚定在表单的底部,但不是表单的一部分。没关系,因为你不想提交表格。。。您希望从表单中获取所有par,然后使用jQuery或javascript hhtp post/get请求将值ajax发送到服务器。
既然你想使用jQuery,就做如下的事情
<input type="text" name="nameField" id="nameInput" /> <br>
<a href="#?w=500" rel="popup_name" id="popLight">test</a> <br>
<script>
$(document).ready(function() {
$('.popLight').click(function() {
//Get the form input value
var nameInput = $('#nameInput').val();
//Get the anchor attributes
var href = $('#popLight').attr('href');
//Do your stuff with the attr parameters, if you really need this here
//...
//Send your stuff to the server
$.post("recep.php", { pars: "Your pars here"},
function(receivedData) {
//open whatever you want with the html from the server
$('#whateverId').html('receivedData');
});
});
});
</script>
要使用窗体检索值,只需标识窗体并从函数调用它
var$input=$('35; form:input').val();