当我想在某个事件被触发后阻止其他事件处理程序执行时,我可以使用两种技术之一.我将在示例中使用jQuery,但这也适用于plain-JS:
event.preventDefault()
$('a').click(function (e) {
// custom handling here
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
return false
$('a').click(function () {
// custom handling here
return false;
});
Run Code Online (Sandbox Code Playgroud)
这两种停止事件传播的方法之间有什么显着差异吗?
对我来说,return false;
比执行方法更简单,更短并且可能更不容易出错.使用该方法,您必须记住正确的套管,括号等.
另外,我必须在回调中定义第一个参数才能调用该方法.也许,有一些原因可以解释为什么我应该避免这样做并使用preventDefault
呢?有什么更好的方法?
javascript jquery event-handling event-propagation dom-events
如果在没有字段中的任何数据的情况下按下发送按钮,我将如何防止页面刷新?
验证设置正常,所有字段都变为红色,然后页面立即刷新.我对JS的了解相对基础.
特别是我认为processForm()
底部的功能是"坏".
HTML
<form id="prospects_form" method="post">
<input id="form_name" tabindex="1" class="boxsize" type="text" name="name" placeholder="Full name*" maxlength="80" value="" />
<input id="form_email" tabindex="2" class="boxsize" type="text" name="email" placeholder="Email*" maxlength="100" value="" />
<input id="form_subject" class="boxsize" type="text" name="subject" placeholder="Subject*" maxlength="50" value="FORM: Row for OUBC" />
<textarea id="form_message" class="boxsize" name="message" placeholder="Message*" tabindex="3" rows="6" cols="5" maxlength="500"></textarea>
<button id="form_send" tabindex="5" class="btn" type="submit" onclick="return processForm()">Send</button>
<div id="form_validation">
<span class="form_captcha_code"></span>
<input id="form_captcha" class="boxsize" type="text" name="form_captcha" placeholder="Enter code" tabindex="4" value="" />
</div>
<div class="clearfix"></div>
</form>
Run Code Online (Sandbox Code Playgroud)
JS
$(document).ready(function() {
// …
Run Code Online (Sandbox Code Playgroud) 嗨,我有一个页面,让用户查看特定锦标赛和圆形的结果
用户将根据运动选择选择运动然后比赛,然后用户将选择基于比赛选择填充的比赛
完成所有操作后,用户按"提交"按钮,将根据锦标赛和所选轮次查找结果
我的代码工作得很好:
mainPage.php
<script type="text/javascript">
$(document).ready(function()
{
$(".sport").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_sport.php",
dataType : 'html',
data: dataString,
cache: false,
success: function(html)
{
$(".tournament").html(html);
}
});
});
$(".tournament").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_round.php",
data: dataString,
cache: false,
success: function(html)
{
$(".round").html(html);
}
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
get_sport.php
<label>Sport :</label>
<form method="post">
<select name="sport" class="sport">
<option selected="selected">--Select Sport--</option>
<?php
$sql="SELECT distinct …
Run Code Online (Sandbox Code Playgroud) 我在尝试将值从 HTML 表单<input>
元素传递到表单的action
属性并将其发送到 FastAPI 服务器时遇到以下问题。
这是 Jinja2 (HTML) 模板的加载方式:
# Test TEMPLATES
@app.get("/test",response_class=HTMLResponse)
async def read_item(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
Run Code Online (Sandbox Code Playgroud)
我的 HTML 表单:
<form action="/disableSubCategory/{{subCatName}}">
<label for="subCatName">SubCategory:</label><br>
<input type="text" id="subCatName" name="subCatName" value=""><br>
<input type="submit" value="Disable">
</form>
Run Code Online (Sandbox Code Playgroud)
我的 FastAPI 端点将在表单操作中调用:
<form action="/disableSubCategory/{{subCatName}}">
<label for="subCatName">SubCategory:</label><br>
<input type="text" id="subCatName" name="subCatName" value=""><br>
<input type="submit" value="Disable">
</form>
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
"GET /disableSubCategory/?subCatName=Barber HTTP/1.1" 404 Not Found
Run Code Online (Sandbox Code Playgroud)
我想要实现的是以下 FastAPI 调用:
/disableSubCategory/{subCatName} ==> "/disableSubCategory/Barber"
Run Code Online (Sandbox Code Playgroud)
谁能帮助我理解我做错了什么?
谢谢。狮子座
javascript ×3
jquery ×3
forms ×2
html ×2
ajax ×1
dom-events ×1
fastapi ×1
jinja2 ×1
php ×1
python ×1