【问题标题】:Detect how many times the users have click the button检测用户点击按钮的次数
【发布时间】:2011-02-24 22:49:18
【问题描述】:

只是想知道是否有一种方法可以通过使用 Jquery 来检测用户单击按钮的次数。

我的主应用程序有一个按钮,可以根据用户添加输入字段。他/她可以根据需要添加任意数量的输入字段。当他们提交表单时,添加页面会将数据添加到我的数据库中。我目前的想法是创建一个隐藏的输入字段并将值设置为零。每次用户单击按钮时,jquery 都会更新隐藏输入字段值的属性。然后“添加页面”可以检测循环时间。请参阅下面的示例。

我只是想知道是否有更好的做法来做到这一点。感谢您的帮助。

主页

<form method='post' action='add.php'>

//omit
    <input type="hidden" id="add" name="add" value="0"/>
    <input type="button" id="addMatch" value="Add a match"/>
//omit

</form>

jquery

$(document).ready(function(){

    var a =0;       
    $("#addMatch").live('click', function(){
        //the input field will append //as many as the user wants.
        $('#table').append("<input name='match"+a+"Name' />");

        a++;

        $('#add').attr('value', 'a');  //pass the a value to hidden input field

        return false;
    });
});

添加页面

$a=$_POST['a']; //

for($k=0;$k<$a;$k++){
    //get all matchName input field
    $matchName=$_POST['match'.$k.'Name'];

    //insert the match
    $updateQuery=mysql_query("INSERT INTO game (team)
                                          values('$matchName')",$connection);

    if(!$updateQuery){
        DIE('mysql Error:'+mysql_error());
    }
}

【问题讨论】:

    标签: php jquery mysql forms click


    【解决方案1】:

    $('#add').attr('name', 'a').val(a); ????

    这不正确,你应该使用:

    $('#add').attr('value', a);
    send the content of the "a" variable to the "value" property of element with ID "add"
    

    我相信这就是你想要做的......

    【讨论】:

    • 我不知道为什么,但你的代码创建 不过感谢您的回复。 +1
    • 嗨,它正在多个项目中进行生产...您是否将值正确发送到“a”变量? var a = 某物; $("#add").attr("值", a);
    【解决方案2】:

    我有点困惑。在此之后:

    $('#add').attr('name', 'a');  //pass the a value to hidden input field
    

    您不应该实际存储a 的值吗?

    $('#add').attr('name', 'a').val(a);
    

    【讨论】:

    • 是的。我错过了。谢谢。我应该让它 $('#add').attr('value', 'a').val(a);
    猜你喜欢
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    相关资源
    最近更新 更多