我正试图将一个值从输入字段传递到其他页面。不幸的是,该值无法检索/不起作用,所以希望有人能帮助我。
page.php
<form action="" id="testform" method="post">
<select size="1" class="dropdown">
<?php // dropdown options (pages)
$Args = array ('cat' => 1);
$loop = new WP_Query( $Args );
while($loop->have_posts()) : $loop->the_post(); ?>
<option value="<?php echo get_permalink(); ?>">
<?php the_title(); ?>
</option>
<?php endwhile; ?>
</select>
<input id="year" name="year" type="text" value=""/><!--The value that will be passed to another page -->
<input class="button" name="submit" type="submit" value="ENTER"/>
</form>
提交时-转到位置(表单操作)
$(".button").click(function(){
var linkadress = $('.dropdown option:selected').val();
window.location = linkadress;
return false;
});
Footer.php
问题:传递值(字段id=年份)
var yearfield = "<?php echo htmlspecialchars($_POST['year']); ?>";
alert(yearfield); // Currently no value is passed
基于wordpress platform
构建。该表单在所有页面上都可见。汇总表单时,应在新页面(所选下拉位置)上检索'year'
值。代码的其余部分运行良好。
您不是在提交(张贴)表单,而是在单击按钮时重定向。因此PHP从未收到$_POST。
要么必须在函数中使用submit()
,要么必须将年份变量添加到url中。
$(".button").click(function(){
var linkadress = $('.dropdown').val();
$("#testform").setAttribute('action', 'linkadress');
$("#testform").submit();
//Now you can use $_POST['year'] in linkadress
return false;
});
或
$(".button").click(function(){
var linkadress = $('.dropdown option:selected').val();
var year = $().val('#year');
window.location = linkadress + '?year=' + year;
//Now you can use $_GET['year'] in linkadress
return false;
});
您必须获得的值,选择而不是选项。(此外,最好为select设置一个ID,并使用jquery ID选择器而不是类选择器。)
$(".button").click(function(){
var linkadress = $('.dropdown').val();
window.location = linkadress;
return false;
});
是否要根据下拉列表的值更改表单操作,如果是,那么;
$("#testform").submit(function(){
var linkadress = $('.dropdown option:selected').val();
$(this).attr('action', linkadress);
});