[英]How to put a jQuery code into one file which will be referenced by all pages?
我有一个登录弹出窗口,它将在我网站的每个页面上弹出。 我要做的是,一旦用户单击“提交”,就拥有一个JS文件,用于处理该请求的jQuery代码将保存在其中,并进行AJAX调用以验证数据库中的参数。
我能够弹出这个弹出框。 并加载表格。 我认为我的jQuery代码将保存在单独的导入文件中,如下所示:
<script type="text/javascript" >
$(function()
{
$("input[type=submit]").click(function()
{
var some_params= $("#param").val();
var dataString = 'Some url to send to ajax';
if( params validated ok )
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "/problems/add_problem.php",
dataType: "json",
data: dataString,
success: function(json)
{
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
</script>
所以我的问题是,仅当提交正确的表单时,如何使它被调用? 该表单将具有一些id =“ some_name”,但我真的不明白如何仅在调用该表单元素时如何执行此jQuery代码。
这是我要在弹出窗口中显示的表单:
<?php
echo '<div id="login_div">
<form id="login_form" method="post" action="">
<p>
<label for="name"><span>Your Email:</span></label> <input type="text" name="email" />
</p>
<p>
<label for="name"><span>Your Password:</span></label> <input type="password" name="user_pass">
</p>
<p>
<input type="submit" value="Log In" />
</p>
</form>
</div>
<p>
<a href="http://www.problemio.com/auth/create_profile.php">Create Account</a> | <a href="http://www.problemio.com/auth/forgot_password.php">Reset Pass</a>
</p>
';
?>
这是带有jQuery来处理登录表单提交的问题io.js内容:
// javascript library
// login_form
$(function()
{
$("#login_form input[type=submit]").click(function()
{
console.log("test");
alert("1");
// var name = $("#problem_name").val();
// var problem_blurb = $("#problem_blurb").val();
// var dataString = 'problem_name='+ name + '&problem_blurb=' + problem_blurb;
// if(name=='' || problem_blurb == '')
// {
// $('.success').fadeOut(200).hide();
// $('.error').fadeOut(200).show();
/// }
// else
// {
// $.ajax({
// type: "POST",
// url: "/problems/add_problem.php",
// dataType: "json",
// data: dataString,
// success: function(json)
// {
// $('.success').fadeIn(200).show();
// $('.error').fadeOut(200).hide();
//
/// // Here can update the right side of the screen with the newly entered information
// //alert (json);
//
// new_string = "<h2>Most Recently Added Problems</h2>";
// Have to figure out how to make this work with the DOM.
// }
// });
// }
return false;
});
});
您可以使用.submit()将处理程序附加到表单提交事件。 首先,您需要通过ID选择表单:
$("#some_form_id").submit(function() {
// the code you have in the click event above goes here.
});
两件事情。 首先,将上面的代码放入单独的javascript文件时,请确保删除<script ..>和</ script> HTML标签。
接下来,更改以下行:
$("input[type=submit]").click(function()
改为说:
$("#loginform input[type=submit]").click(function()
然后在<form>标记上设置id =“ loginform”。
您可以指定要触发jquery的表单。 http://api.jquery.com/submit/
如果不确定,请右键单击该网页并阅读其html代码。
<script type="text/javascript" src="some.js"></script>
而且,将该功能绑定到form.submit比将其绑定到Submit按钮要好得多。
$('formid').submit(function(){blablabla;return false;})
如果您想在不使用ID的情况下处理页面上每个提交的click事件,则始终可以在click事件中使用this
关键字来查找发件人,然后找到父form
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.