我有一个 <form>
当前将输入字段数据发送到 Controller 中的 1 个函数。看起来像这样。
<?php echo form_open('moneyexchange/invest_first_page'); ?>
<input type="text" placeholder="Amount in €" name="writtenamount"/>
<button class="btn btn-lg" type="submit" >Invest</button>
<button class="btn btn-lg" type="submit" >Borrow</button>
<?php echo form_close();?>
问题是我需要 Invest 按钮转到货币兑换 Controller 内的 invest_first_page 函数,而 Borrow 按钮转到同一 Controller 内名为 borrow_first_page 的不同函数。我需要使用一个输入字段,这是必须的,所以不能使用两个输入字段,然后他们转到函数,他们被重定向到两个不同的页面,所以有什么办法让这个表单发布输入值单击不同的按钮时到两个不同的 Controller 。
只是为了获得额外信息,我在 Controller 中也发布了按钮所指向的功能。
public function invest_first_page(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->view('header');
$userProvidedAmount = $this->input->post("writtenamount");
$data = array(
'userProvidedAmount' => $userProvidedAmount
);
$this->load->view("invest_firstpage", $data);
$this->load->view('footer');
}
public function borrow_first_page(){
}
我可以使用 JavaScript 来做吗?
最佳答案
下面的例子
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$( document ).ready(function() {
$('#yourbuttonid').click(function() {
$.ajax({
url: 'moneyexchange/invest_first_page',
type: 'POST',
data: { writtenamount: $("#amount").val()},
success: function (result) {
// do stuff
}
});
});
});
</script>
</head>
<body>
<?php echo form_open('moneyexchange/invest_first_page'); ?>
<input type="text" placeholder="Amount in €" name="writtenamount"/>
<button id="yourbuttonid" class="btn btn-lg" type="submit" >Invest</button>
<button class="btn btn-lg" type="submit" >Borrow</button>
<?php echo form_close();?>
</body>
</html>
关于javascript - 使用 2 个按钮获取 1 个输入字段值到 2 个不同的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34792629/