找出单击按钮时发生的事情的最简单方法是什么?我知道F12调试器的基础知识,但除此之外,我有点无能为力。我已经读过关于事件处理程序等但从未使用它们,所以希望有一种更简单的方法来跟踪它。
这是按钮代码。看起来很好,它可以做它应该做的事情,但也有额外的不良行为。
$(document).on('click', '.friendly_submit', function () {
// Work out our team ID.
var OurTeamIDF = $('div').find('[data-ourteamid]');
var OurTeamID = OurTeamIDF.data('ourteamid');
var OpponentID = $(this).closest('form')
.find('.friendly_opponent')
.find(':selected').attr('data-OpponentID');
var Venue = $(this).closest('form')
.find('.friendly_venue')
.find(':selected').text();
var WhichFriendly = $(this).closest('form').attr('data-WhichFriendly');
$.post('scripts/randomfriendly.php',
{
OurTeamID : OurTeamID,
OpponentID : OpponentID,
Venue : Venue,
WhichFriendly : WhichFriendly
},
function (data) {
console.log(data);
}
)
});
HTML代码如下。
echo "<form data-WhichFriendly = 0 class = 'box_center center'>";
echo "<fieldset class = 'random_friendly_fieldset box_center'>";
echo "<legend>Random Friendly</legend>";
//echo "<p class = 'bold center underline'>Choose Opponent</p>";
echo "Choose Opponent : <select class = 'friendly_opponent'>";
foreach ($AllTeamsArray as $Team) {
// Don't include ourselves in the list.
if (trim($Team['Name']) <> trim($OurTeamName)) {
$TeamID = $Team['ID'];
echo "<option data-OpponentID = $TeamID>" . $Team['Name'] . " (" . $Team['League'] . ")</option>";
}
}
echo "</select></br></br>";
echo "Venue : <select class = 'friendly_venue'>";
echo "<option>Neutral</option>";
echo "<option>Home</option>";
echo "<option>Away</option>";
echo "</select></br></br>";
echo "<button class = 'friendly_submit'>Submit</button>";
echo "</fieldset>";
echo "</form>";
答案 0 :(得分:2)
我想到了两件事:
1)按钮为type="submit"
并且正在提交表单(导致页面刷新)
2)按钮是设置为href="#'
修改强>
现在您已经发布了代码,我正在更新我的答案。
更改您的代码,以便不是在按钮上捕获点击事件,而是在表单上捕获提交事件。
$( "#add-form-id-here" ).submit(function( event ) {
event.preventDefault();
//change your handler so you get the correct values etc here.
});
然后确保在按钮上添加type="submit"
。
答案 1 :(得分:1)
尝试添加:
event.preventDefault();
event.stopPropagation();
^\/projects(?!.*jobs|.*members).*$