I have a basic textarea in a form, and a link in another section of the page. What I'd like to do is if the user inputted any value in the textarea before clicking the link, to save this value in, say, $_SESSION['value'] so that I can re-enter it when they come back.
<body>
<form method="post" action="sent.php">
<textarea id="text" name="text"></textarea>
<input type="submit">
</form>
...
<div>
<a id="test" href="away.php">Go here</a>
</div>
...
<script type="text/javascript">
$("#test").click(function(){
var value = $('#text').val();
//pass this value in somewhere before I'm redirected?
});
</script>
</body>
答案 0 :(得分:2)
实际上非常简单。您只需更新链接的href
即可。重定向不会发生,直到点击功能完成 -
$("#test").click(function(event) {
var value = $('#text').val();
$("#test").attr("href", $("#test").attr("href") + "?text=" + $("#text").val());
});
答案 1 :(得分:2)
每次离开textarea时都可以将其保存到会话中,如果在页面加载时存在则将其加载回来
$('#textarea').focusout(function(){
//save to session
$.post('url', {val:$('#textarea').val()});
});
答案 2 :(得分:1)
使用以下代码。
<a href="#" name="test">Go Here</a>
JQuery代码
$(function(){
$(document).on('click', 'a[name=test]', function(){
var textareaContent = $("#text").val();
var data = {
content:textareaContent
};
$.post('url', data, function(response){
window.location.href="away.php";
});
});
});