Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过ajax发布到数据库_Javascript_Jquery - Fatal编程技术网

Javascript 通过ajax发布到数据库

Javascript 通过ajax发布到数据库,javascript,jquery,Javascript,Jquery,我有一个js函数,可以根据select输入字段中的值更新数据库。 这是javascript函数: function getStatusAction() { var matchId = $(this).attr('match-id'); var status = jQuery('#match-status').val(); jQuery.ajax({

我有一个js函数,可以根据select输入字段中的值更新数据库。 这是javascript函数:

            function getStatusAction()
            {
              var matchId = $(this).attr('match-id');
              var status = jQuery('#match-status').val();
              jQuery.ajax({
                url:'../core/match_stat.php',
                type:'POST',
                data:{matchId:matchId,status:status},
                success: function(data){
                    //alert("Ok");
                },
                error: function(){
                    alert("Error..");
                },
              });
            }
            jQuery('select[name="match-status"]').change(getStatusAction);
html的一部分:

                <tr>
                    <td>
                        <select class="input" name="match-status" match-id="<?=$ActiveMatch['id'];?>" id="match-status">
                            <option value="3" <?= (($ActiveMatch['status'] == '3')?'selected':'')?>> </option>
                            <option value="1" <?= (($ActiveMatch['status'] == '1')?'selected':'');?> >won</option>
                            <option value="2" <?= (($ActiveMatch['status'] == '2')?'selected':'');?> >lost</option>
                        </select>
                    </td>
                </tr>
该函数应该获取匹配的id,例如1,2。。。和状态,如1、2、3,分别为松散状态、胜利状态和默认状态。 问题是我只能更新第一行数据。如果我尝试对另一行数据执行此操作,将使用第一次调用中的值。例如,如果我将第一行状态更新为win,如果我尝试将第二行状态更新为LOSE,则更新为win。上一个操作的状态。 我怎样才能解决这个问题

你可以试试

        function getStatusAction(Element)  //<< add parameter here 
        {
          var matchId = $(Element).attr('match-id');  // <<< use it here
          var status = jQuery('#match-status').val();
          jQuery.ajax({
            url:'../core/match_stat.php',
            type:'POST',
            data:{matchId:matchId,status:status},
            success: function(data){
                //alert("Ok");
            },
            error: function(){
                alert("Error..");
            },
          });
        }
        jQuery('select[name="match-status"]').on('change' , function(){
           getStatusAction(this);  // << use the function here
        });
注意:id应该是唯一的,所以如果只有一个元素的id状态匹配,那么它就可以了。。但是如果您的选择具有相同的id,则需要将id=match status更改为class=match status,并将var status=jQuery'match-status'.val;to var status=$Element.val