我已經通過向jQuery網站添加條件來修改此代碼。但無論結果如何,它總是會在第一個'如果'中出現。我怎樣才能讓自己的情況起作用?
<script>
/* attach a submit handler to the form */
$("#searchForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $(this),
term = $form.find('input[name="s"]').val(),
url = $form.attr('action');
/* Send the data using post and put the results in a div */
**//the condition doesn't work here. It always get into the first "if". Why?**
$.post(url, { s: term },
function(data) {
if (var content = $(data).find('#content')) {
console.log('One or more results were found');
} else {
console.log('no result');
}
}
);
});
</script>
我只想提一下,使用有效的html,所有的ID都應該是唯一的,所以你不必使用'$(data).find('#content')',你可以使用'$('#內容')' – nathanjosiah 2012-03-17 06:59:18