找到了解決辦法:jQuery的表單提交功能不能正常工作
我不得不封裝功能的另一功能。這是因爲它與我在我的html文件上的其他Js庫衝突。
大家歡呼!
<script type="text/javascript">
(function($) {
// we can now rely on $ within the safety of our 「bodyguard」 function
$(document).ready(function() { alert("nyah nyah! I’m able to use '$'!!!!");
$("#keywordForm").submit(function(e) { alert('asshole');})});
}) (jQuery);
前一題
我的提交按鈕不做即使我已經形式的jQuery綁定任何東西。
不顯示警報消息,意思是說它甚至沒有進入該功能。
我對這個問題抱有懷疑態度。 :(
<script type="text/javascript">
$(document).ready(function() {
$("form").submit(function(e) {
e.preventDefault()
alert("sometext");
return false;
updateKeywordSubscribed(e);
});
});
function updateKeywordSubscribed(e, keywords) {
e.preventDefault();
var option_form = jQuery(e.target);
$.ajax({
url : option_form.attr('action'),
type : option_form.attr('method'),
data : option_form.serialize(),
dataType : 'json',
success : function(response) {
console.log(response)
var result = response.data
if(result == 'False')
{ alert('Failed')}
alert('some text');
error : function(xhr, ajaxOptions, thrownError) {
// log ajax errors
}
});
}
<!--function DoTheCheck() { {% for price in price_list %}
if($('input[type=checkbox]:checked').length == {{ price.no_of_keywords }})
{ alert('Price to pay is $' + {{ price.price_of_each_keyword }} + '.0'); }
{% endfor %}
}-->
</script>
</head>
<body>
<div class="container">
<!-- header -->
<div id="header-wrap">
<div id="header">
<a name="top"></a>
<h1 id="logo-text"><a href="/" title=""style="padding-left:32px; top: 80px;">Rosebud</a></h1>
<p id="slogan"style="padding-left:32px;letter-spacing:0px;">
Your brand on the web...
</p>
<div id="nav" style="padding-top: 15px;">
<ul>
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/topic/">Topic</a>
</li>
<li>
<a href="/opinion/">Opinion</a>
</li>
<li>
<a href="/statistics/">Statistics</a>
</li>
<li>
<a href="/realtime/">Realtime</a>
</li>
<li>
<a href="/">Support</a>
</li>
<li>
<a href="/about/">About</a>
</li>
<li id="current">
<a href="/profile/">My Profile</a>
</li>
</ul>
</div>
{% if user.is_authenticated %}
<p id="auth" style="margin: 0px;">
<a href="/">Welcome {{ user.username }} </a>
<a href="/accounts/logout">Log Out</a>
</p>
{% else %}
<p id="auth" style="margin: 0px;">
<a href="/accounts/login/">Log In </a>
<a href="/accounts/register/">Register</a>
</p>
{% endif %}
</div>
</div>
<!-- End of Header -->
<div id="content-wrap">
<!-- content -->
<div id="content-home">
<br />
{% block content %} <h2 style="margin: 0px; margin-bottom: 20px;">Profile Page</h2>
{% if user.is_authenticated %}
<p id="auth" style="margin: 0px;">
{% for userprofile in object_list %}
{% if userprofile.username = user.username %}
<table style="margin: 0px; width: 600px; border: 0;">
<tbody>
<tr>
<td style="width: 250px; border: 0;"><label for="first_name">First Name:</label></td>
<td style="border: 0;">{{ userprofile.first_name }}</td>
</tr>
<tr>
<td style="border: 0;"><label for="last_name">Last Name: </label></td>
<td style="border: 0;">{{ userprofile.last_name }}</td>
</tr>
<tr>
<td style="border: 0;"><label for="email">Email: </label></td>
<td style="border: 0;">{{ userprofile.email }}</td>
</tr>
<tr>
<td style="border: 0;"><label for="username">Username: </label></td>
<td style="border: 0;">{{ userprofile.username }}</td>
</tr>
<tr>
<td style="border: 0;"><label for="keywords">Keywords currently subscribed to: </label></td>
<td style="border: 0;">{{ userprofile.keywords_subscribed }}</td>
</tr>
<tr>
<input type="button" name = "submit" id="keywordBtn" value="Click to refresh list of keywords subscribed" action="/refresh_list/" />
<INPUT TYPE=hidden NAME="username" Value = {{ userprofile.username }} >
</tr>
</tbody>
</table>
<br />
<h3 style="margin: 0px; margin-bottom: 20px;">Click the checkboxes for more subscriptions</h3>
<table>
<tbody>
{% for keyword in keyword_list %}
{% if forloop.counter|divisibleby:"3" %}
<tr>
{% endif %}
<td>
{% if keyword.keyword_name == userprofile.keywords_subscribed %}
<input type="checkbox" disabled="disabled" name="keywords" value="keywords"/>
{% else %}
<input type="checkbox" name="cb" value="keywords" />
{% endif %}
{{keyword.keyword_name}}
</td>
{% if forloop.counter|add:"1"|divisibleby:"3" %}
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
<br />
<h3 style="margin: 0px; margin-bottom: 20px;">Price of subscriptions</h3>
<div id = "subscription"></div>
<input type="button" value="Amount to pay!" onclick="DoTheCheck()">
<br />
</br>
<form id="keywordForm" method="post"> <!--action="/keyword_subscribe/"-->
<P>Keyword: <INPUT TYPE="TEXT" NAME="keyword_input" Value = "Enter a new Keyword" class = 'hint'><BR><BR>
<!--<INPUT TYPE=hidden NAME="name_input" Value = {{ userprofile.username }} >-->
<INPUT TYPE="submit" Value="Crawl" name="crawl">
</P>
</form>
而這裏的jQuery的一部分:
<script type="text/javascript">
$(document).ready(function() {
$("#keywordForm").submit(function(e) {
alert("sometext");
return false;
});
});
</script>
感謝gdron!你的答案很好。但我找到了解決方案。請參閱頂部@gdoron – 2012-02-28 04:53:51
@StanwinSiow。你應該提供很好的答案......如果你找到答案,你應該發佈答案,而不是編輯問題。很高興你設法解決它。 – gdoron 2012-02-28 05:50:17