我在 codeigniter 中使用 MVC 框架,我有一个 View ,我有这个链接(请注意,href
值是通过调用 PHP 函数生成的,该函数又采用 'auth/mylogin'
作为参数:
<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin'); ?>"> CLICK ME</a>
现在我在同一个 View 页面上有一个单选按钮。
<input id="ut1" name="usertype" type="radio" value="ut1"<?php echo set_radio('usertype', 'ut1', TRUE); ?> />
<label for="ut1" onclick="">User Type 1</label>
<input id="ut2" name="usertype" type="radio" value="ut2"<?php echo set_radio('usertype', 'ut2'); ?> />
<label for="ut2" onclick="">User Type 2</label>
现在我想要的是:我还想传递用户类型值。我的意思是,根据用户在单击链接之前选择的单选按钮,我希望代码为:
在单击 CLICK ME 之前选择了用户类型 1 radio
<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin/ut1'); ?>"> CLICK ME</a>
在单击“点击我”之前选择了用户类型 2 radio
<a href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin/ut2'); ?>"> CLICK ME</a>
由于没有提交,我假设我必须使用 javascript 来执行此操作。
但是由于 javascript 是客户端而 PHP 服务器端,我是不确定如何完成这项工作。
我如何实现这一点?
请注意,代码已被剥离以仅显示相关部分。
最佳答案
<label for="ut2" onclick="messi_fan('ut2');">User Type 2</label>
<label for="ut1" onclick="messi_fan('ut1');">User Type 1</label>
<a id = "link" href = "<?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin'); ?>"> CLICK ME</a>
<script>
function messi_fan(type){
url = " <?php echo $this->FrontController->myLoginURL(site_url() . 'auth/mylogin/'; ?>";
new_url = url+type;
$("#link").attr('href',new_url);
}
</script>
关于php - 基于单选按钮状态的链接点击动态 href 生成 : PHP interaction with javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16911839/