在AJAX调用时将jQuery对象转换为String

时间:2012-11-21 17:27:02

标签: javascript jquery html ajax

假设我提交的表单中包含user_input值的任何内容,例如“我有空”。我通过AJAX提交它,它作为一个字符串返回给我。好了,现在它是一个对象...我怎么把它变成一个字符串?

谢谢,

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>

    <title>Sin título 3</title>
</head>

<body>

<div id="result_target" style="display:none;"></div>
<form id="form_to_post" action="#" method="post">
<input type="hidden" name="url" value="example.php" />
<input type="text" name="user_input" />
<input type="submit" value="Submit"  />
</form>

<script>
$(document).ready(function() {

$("#form_to_post").submit(function() {

var hiddenURL = $("input[name=url]");
var userInputForm = $("input[name=user_input]");

//var dataString = "userInput="+inputForm;



$.ajax({
type: "POST",
url: hiddenURL;
data: {userInput: userInputForm},
success: function(return_contents) {
//returns jquery object...
var output =//
$("#result_target").html(output);
}
});

});

});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

使用val()从输入中获取值

data: {userInput: userInputForm.val()},