我有我的代码
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var word = $('#word').attr('value');
var sentence = $('#sentence').attr('value');
var email = $('#email').attr('value');
$.ajax({
type: "POST",
url: "/sentence-examples/suggestsentence.php?",
data: "sentence="+ sentence+
"&word="+ word+
"&email="+ email,
success: function(data){
$('form#submit :input').not('input[type="submit"]').val("");
$('div.success').fadeIn();
$('div.success').html(data);
}
});
return false;
});
});
function hide(obj) {
var el = document.getElementById(obj);
el.style.display = 'none';
}
当“word”包含像“half-breed”这样的连字符时,我无法理解的原因是它不会将变量传递给我的脚本。它出现了空白。
任何帮助?
答案 0 :(得分:1)
.attr('value')
,而是使用.val()
。 ( var word = $('#word').val();
)不要手动构造参数字符串,而是使用
data: {
sentence: sentence,
word: word,
email: email
}
?
选项data: