jquery选择器中的动态ID不起作用

时间:2017-10-12 05:19:02

标签: javascript php jquery

我尝试了这个,但是ajax调用了两次PHP文件或Diplaying其他部分两次。请帮我。我在按钮

中调用了editCat()

编辑HTML

<button class="btn btn-primary btn-xs" onclick="editCat(<?php echo $row['id']; ?>)">
    <span class="glyphicon glyphicon-edit"></span>
</button>
<div id="edittextdiv_<?php echo $row['id']; ?>">
    <input type="text" name="" placeholder="Enter categorie name" id="edittextinput_<?php echo $row['id'];?>" value="<?php echo $row['name']; ?>">
</div>

JS

function editCat(id) {
    $('#edittextdiv_'+id+'').toggle();
    $('#edittextinput_'+id+'').keyup(function(event){
        var category = $(this).val();
        var firstletter = category.charAt(0);
        var capitalreg = new RegExp(/^[A-Z]*$/);
        if(capitalreg.test(firstletter) && category != ''){
            if(event.keyCode == 13){
                $.ajax({
                    url:"edit_cat.php",
                    type:"post",
                    dataType:"json",
                    data:{category:category, item_id:id},
                    success:function(response) {
                        if (response.success == true) {

                        }
                    }
                });
            } 
        } else {
            $('#edittextinput_'+id+'').after("<p class='text-danger'>Enter first letter in capital</p>");
        }
    });
}

1 个答案:

答案 0 :(得分:0)

你可以避免使用
     $('#edittextinput_'+id+'').keyup()  因为没有必要在另一个函数中调用keyup函数

请在输入按钮

中调用onkeyup javascript函数
  <input type="text" name="" placeholder="Enter categorie name" id="edittextinput_<?php echo $row['id'];?>" value="<?php echo $row['name']; ?>" onkeyup="editCat('<?php echo $row['id'];?>',this)" >

请参阅我的代码段

function check_key(id,cc,event) {

        /*$('#edittextdiv_'+id).toggle(); toggle */
        var x = event.which || event.keyCode;
        var jq = $(cc);
        var category =jq.val();
        var firstletter = category.charAt(0);
        var capitalreg = new RegExp(/^[A-Z]*$/);
        if(capitalreg.test(firstletter) && category != ''){
            
            if(event.keyCode == 13){
            alert('ajax code');
                $.ajax({
                    url:"edit_cat.php",
                    type:"post",
                    dataType:"json",
                    data:{category:category, item_id:id},
                    success:function(response) {
                          
                        if (response.success == true) {

                        }
                    }
                });
            } 
        }else {
            /*alert('else code')*/
            $('#edittextinput_'+id).after("<p class='text-danger'>Enter first letter in capital</p>");
        }
}
function editCat(id) {

    $('#edittextdiv_'+id+'').toggle();
    
        var category = $('#edittextinput_'+id+'').val();
        var firstletter = category.charAt(0);
        var capitalreg = new RegExp(/^[A-Z]*$/);
        if(capitalreg.test(firstletter) && category != ''){
                $.ajax({
                    url:"edit_cat.php",
                    type:"post",
                    dataType:"json",
                    data:{category:category, item_id:id},
                    success:function(response) {
                        if (response.success == true) {

                        }
                    }
                });
        } else {
            $('#edittextinput_'+id+'').after("<p class='text-danger'>Enter first letter in capital</p>");
        }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="edittextdiv_1">
    <input type="text" name="1" placeholder="Enter categorie name" id="edittextinput_1" onkeyup="check_key('1',this,event)" value="test">
</div><br>
 <button class="btn btn-primary btn-xs" onclick="editCat(1)">
    test
</button>