更改form_open路径(代码点火器)


Change form_open path (CodeIgniter)

>我这里有一个简单的形式,

<?php echo form_open('moneyexchange/invest_first_page');  ?>
     <input id="amount" value="" type="text" placeholder="Amount in &euro;"  name="writtenamount"/>
     <button class="btn btn-lg" type="submit" id="yourbuttonid" >Invest</button> 
     <button class="btn btn-lg" type="submit" id="yourbuttonid2">Borrow</button>
<?php echo form_close();?>
我想

做的是提交每个按钮,我想更改表单的路径,如果我在"投资"按钮上按提交,它会转到此路径moneyexchange/invest_first_page,当我单击借按钮时,我需要它更改路径,因为我同时使用一个输入字段。现在它仅在我单击投资时才有效。请详细告诉我步骤。

你可以使用 jquery 来做到这一点。

<script type="text/javascript">
    $('#yourbuttonid').click(function() { 
        $.ajax({
            url: 'moneyexchange/invest_first_page',
            method: 'POST',
            data: { writtenamount: $("#amount").val()},
            success: function (result) {
              // do stuff
            }
        });  
    });
</script>

对另一个按钮执行相同的操作,但在脚本中只需将 id 更改为 yourbuttonid2

当您单击借用时,将调用以下函数。你可以做类似的事情。

<script type="text/javascript" language="javascript">
 $(document).ready(function(){
        $("#yourbuttonid2").click(function(e) {
            e.preventDefault();
            var selectedMark = $("#id").val(); //get value here
                $.ajax({
                    type: 'GET',
                    cache:false,
                    timeout:10000,
                    async:true,
                     url: "<?php echo base_url().'controller/function_name' ?>",
                    data:{'val1':selectedMark,'val2':val},
                    success:function(response)
                    {

                    },
                   error:function(jqXHR, textStatus, errorThrown )
                    {
                    console.log(textStatus);
                        alert(textStatus);
                    console.log(errorThrown );
                    }
                });
        });
    });
</script>

我要做的是捕获提交按钮的单击事件并检查其ID或类名或内容是什么,然后更改表单路径。更改表单路径后,可以触发表单的提交。

在(伪)代码中:

$('.btn-frm').click(function(e){ //this is common to all submit btns in the form
  e.preventDefault(); //this stops the form from submitting
  var btnId = $(this).attr('id') //get the btn id
  if(btnId == 'investidname'){
    //change form action invest, could use $('formid').attr('action', 'pathtoend');
  }else if(btnId == 'borrowname'){
   //change form action borrow, could use $('formid').attr('action', 'pathtoend');
  }
  $('#formid').submit();//since, we have changed the path, submit it
});

附JSFIDDLE:https://jsfiddle.net/4ueokvL4/2/

希望对您有所帮助!