2单击事件在jquery中不起作用

时间:2015-02-12 08:05:38

标签: javascript jquery

我需要你的支持,因为我试图添加2个点击事件,如下所示,但第二部分只有工作,第一部分没有工作,直到删除第二部分

<script>
    $(document).ready(function () {
                $(function () {
                $("#add_by_serial").click(function () {
                alert("Hello");
                /* var newquantity = $("#serial_newquantity").val();
                var quantityno = $("#serial_quantityno").html();
                var prodcutname = $("#serial_prodcutname").html();
                var serialnumber = $("#serial_serialnumber").html();
                var productid = $("#serial_productid").html();
                var search_by_serial = 1;

                $("#query_result").load("stock/save_edited_quantity.php", { newquantity: newquantity, search_by_serial: search_by_serial, quantityno: quantityno, serialnumber: serialnumber, prodcutname: prodcutname, productid: productid}); */
    })
    })  
    })  
    $(document).ready(function () {

        $(function () {
                $('#add_button').click(function () {

                var product_id = <?php echo $_POST['prod_name']; ?>;
                var quantity = $("#newquantity").val();
                var quantityno = $("#quantityno").html();
                var prodcutname = $("#prodcutname").html();
                var serialnumber = $("#serialnumber").html();
                $("#query_result").load("stock/save_edited_quantity.php", { quantity: quantity, product_id: product_id, quantityno: quantityno, serialnumber: serialnumber, prodcutname: prodcutname});
    })
    })
    })


</script>   

3 个答案:

答案 0 :(得分:1)

添加所有活动 $(function(){ });$( document ).ready(function() { });

示例:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="add_by_serial">add_by_serial </button>
<button id="add_button">add_button</button>
<script>
    $(document).ready(function () {
                
               $("#add_by_serial").click(function () {
                  alert("بسم الله الرحمن الرحيم1");

                });
  
              $('#add_button').click(function () {
                  alert("بسم الله الرحمن الرحيم2");
  
               });
  

    })


</script>   

jQuery API文档:jQuery/ready

答案 1 :(得分:0)

试试这个,你应该在同一个ready事件中定义:

<script>

$(document).ready(function () {

    $("#add_by_serial").click(function () {
        alert("Hello");
        /* var newquantity = $("#serial_newquantity").val();
        var quantityno = $("#serial_quantityno").html();
        var prodcutname = $("#serial_prodcutname").html();
        var serialnumber = $("#serial_serialnumber").html();
        var productid = $("#serial_productid").html();
        var search_by_serial = 1;
        $("#query_result").load("stock/save_edited_quantity.php", { newquantity: newquantity, search_by_serial: search_by_serial, quantityno: quantityno, serialnumber: serialnumber, prodcutname: prodcutname, productid: productid}); */
    })  

    $('#add_button').click(function () {
        var product_id = <?php echo $_POST['prod_name']; ?>;
        var quantity = $("#newquantity").val();
        var quantityno = $("#quantityno").html();
        var prodcutname = $("#prodcutname").html();
        var serialnumber = $("#serialnumber").html();
        $("#query_result").load("stock/save_edited_quantity.php", { quantity: quantity, product_id: product_id, quantityno: quantityno, serialnumber: serialnumber, prodcutname: prodcutname});
    })
})  

</script>   

答案 2 :(得分:0)

你有一个变形的剧本。请阅读有关javascript和jQuery语法的内容。我认为你试图将点击事件设置为加载网页上的两个不同的按钮,脚本应该是这样的:

<script type="text/javascript">
    $(document).ready(function () {

       $("#add_by_serial").click(function () {
       alert("بسم الله الرحمن الرحيم");
       });

       $('#add_button').click(function () {
                var product_id = <?php echo $_POST['prod_name']; ?>;
                var quantity = $("#newquantity").val();
                var quantityno = $("#quantityno").html();
                var prodcutname = $("#prodcutname").html();
                var serialnumber = $("#serialnumber").html();
                $("#query_result").load("stock/save_edited_quantity.php", { quantity: quantity, product_id: product_id, quantityno: quantityno, serialnumber: serialnumber, prodcutname: prodcutname});
       });
    });  
</script>   

此致