开发者

val(); returning nothing

Using jQuery... I'm trying to get the val(); of a textarea... and it's not working. When I click on submit (that works!) the alert shows as You said: :(

What am I doing wrong?

<script type="text/javascript" src="jquery-1.5.1.min.js"></script>
<script type="text/javascript" >
$(function() {

$(".comment_button").click(function() {

var te开发者_JAVA百科st = $("#content").val();
var dataString = 'content='+ test;
alert('You said: ' + test);

});
});
</script>
<style>
#error, #success { display:none;}
</style>
<?php include 'inc/main.php'; ?>

<h1>Testing</h1>
<div id="error">Error...</div>
<div id="success">Success...</div>
<form name="form" action="ask.php" method="post">

<textarea rows="5" cols="50" name="content" id="content" name="thequestion"></textarea><br /><br />

<input name="submit" value="Submit!" type="submit" class="comment_button" />

</form>
<div id="flash"></div>
<div id="display"></div>


I modified your code a little and it worked for me: http://jsfiddle.net/EPMy2/2/.

The most major changes that I made were using the .submit() trigger, not .click(), as .click() doesn't allow you to (easily, if at all) prevent the form from submitting.

Here's the modified chunks:

$("#comment_form").submit(function() {
    var test = $("#content").val();
    var dataString = 'content=' + test;
    alert('You said: ' + test);

    return false; //To prevent submit.
});

And this:

<form name="form" id="comment_form" action="ask.php" method="post">


try $("#content").text() instead :-)


Textarea tags don't have "value" attributes, only input tags do. Use .text() or .html() instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜