我有一个表单,其中有图像提交类型的数量。每个图像都有不同的标题。我需要找出点击图像的标题。但是我在表单提交中的点击功能不起作用。
我的表格是:
<form action='log.php' id='logForm' method='post' >
<?
for($j=1;$j<=5;$j++)
{
?>
<input type="image" src="<?=$img;?>" title="<?=$url;?> id="<?="image".$j?> class="images" />
<?
}
?>
</form>
Jquery:
$("#logForm").submit(function(e)
{
$(".advt_image").click(function(event) {
var href=event.target.title;
});
var Form = { };
Form['inputFree'] = $("#inputFree").val();
// if($("#freeTOS").is(":checked"))
Form['freeTOS'] = '1';
$(".active").hide().removeClass('active');
$("#paneLoading").show().addClass('active');
var url="http://"+href;
$.post('processFree.php', Form, function(data)
{
if(data == "Success")
{
$("#FreeErrors").html('').hide();
swapToPane('paneSuccess');
setTimeout( function() { location=url }, 2500 );
return;
}
swapToPane('paneFree');
$("#FreeErrors").html(data).show();
});
return false;
});
如何获取此$("#logForm").submit(function())
中点击图像的标题值?
我如何使用点击图像的 id?
您可以使用
event.target
属性
$("#logForm").submit(function(e)
alert($(e.target).attr('title'));
});
http://api.jquery.com/event.target/
[更新]
我只是意识到这行不通。我认为没有一个简单的解决方案。您必须跟踪输入上的单击事件,并在以后使用它。
jQuery 提交,我怎么知道按下了什么提交按钮?
$(document).ready(function() {
var target = null;
$('#form :input[type="image"]').click(function() {
target = this;
alert(target);
});
$('#form').submit(function() {
alert($(target).attr('title'));
});
});
[更新 2] - .focus
不起作用,但.click
正在工作http://jsfiddle.net/gjSJh/1/
在我看来,您有多个提交按钮。不要在提交时调用该函数,而是在单击这些按钮时调用它,以便您可以轻松访问用户选择的按钮:
$('input.images').click(function(e) {
e.preventDefault(); //stop the default submit from occuring
alert($(this).attr('title');
//do your other functions here.
});
//所有元素的运行时单击事件
$(document).on('vclick', '.control', function (e) { // .control is classname of the elements
var control = e.target;
alert(e.currentTarget[0].id);
});
如果您在警报中没有收到正确的消息,只需使用 Firebug 进行调试。
检查以下代码,您可以获得点击图像的标题。
单击一下
$(document).ready(function()
{
$('#logForm').submit(function(e){
$(".images").click(function(event) {
alert(event.target.title);
});
return false;
});
});
双击
$(document).ready(function()
{
$('#logForm').submit(function(e){
$(".images").dblclick(function(event) {
alert(event.target.title);
});
return false;
});
});
在
渲染页面中添加以下ondomready
。$(document).ready(function(){
$("form input[type=image]").click(function() {
$("input[type=image]", $(this).parents("form")).removeAttr("clicked");
$(this).attr("clicked", "true");
});
});
现在,在表单的提交操作中添加以下行为和 yupeee...没问题!。。。。
$("#logForm").submit(function(e)
{
var title = $("input[type=image][clicked=true]",e.target).attr("title");
.....
.....
});