【问题标题】:How to change part of url (ID) using radio button?如何使用单选按钮更改部分 url (ID)?
【发布时间】:2014-07-08 19:13:18
【问题描述】:

我在 JSP 中通过循环按下了一些单选按钮:

<input type="radio" name="radioBtn" value="<s:property value="#country.getIdCountry()"/>">

单选按钮的值描述了它选择的国家的 ID。

然后我创建了一些链接,例如:

<a href='addDefaultPoints.action?countryId=<s:property value="#country.getIdCountry()"/>&height=400&width=350'>Link 1</a>           
<a href='editCountryMap.action?height=550&width=750&id=<s:property value="#country.getIdCountry()"/>&keepThis=true&TB_iframe=true'>Link 2</a>

我想将代码更改为一些动态页面,该页面可以在 href 中指定的 url 内更改单选按钮集的 onChange 事件上的 ID。

我尝试使用 Javascript 来实现此功能,但没有成功。也许我需要在服务器端执行此操作。我正在使用 JSP、struts、Jquery 和 Javascript。

这是我尝试过的:

var myGetValue = parseURL("id");
function checkV(f,v){
    for(var i=0;i<c.length;i++){
        c[i].checked=(c[i].value==v)?true:false;
    }
}
checkV(myForm,myGetValue);

【问题讨论】:

  • 你能告诉我们你尝试了什么吗?
  • 发布在最近的编辑上

标签: javascript jquery jsp struts2 radio-button


【解决方案1】:

HTML

<input type="radio" name="radioBtn" value="1">
<input type="radio" name="radioBtn" value="2" checked>
<input type="radio" name="radioBtn" value="3">
<input type="radio" name="radioBtn" value="4">

<a id="lnk1" href='addDefaultPoints.action?height=400&width=350&countryId='>Link 1</a>           
<a id="lnk2" href='editCountryMap.action?height=550&width=750&keepThis=true&TB_iframe=true&id='>Link 2</a>

JS

$(document).ready(function() {
    var countryId = $('input[name=radioBtn]:checked').val();

    $('a#lnk1').attr('href', $('a#lnk1').attr('href') + countryId);
    $('a#lnk2').attr('href', $('a#lnk2').attr('href') + countryId);
});

说明

首先,您通过选中 radioBtn 组的 checked 单选按钮来获取所选国家/地区。 然后确保您的网址的国家/地区参数位于末尾,以便您可以将 countryId 连接到其余链接属性“href”。

结果

链接1:addDefaultPoints.action?height=400&amp;width=350&amp;countryId=2

链接2:editCountryMap.action?height=550&amp;width=750&amp;keepThis=true&amp;TB_iframe=true&amp;id=2

如果参数不是链接末尾的广告,那么您可以使用 replace() 函数,就像 Bhushan Kawadkar 提到的那样。

【讨论】:

  • 这看起来很不错,但是 $('a#lnk1').attr('href', $('a#lnk1').attr('href') + countryId);没有按预期工作。对我来说,ID 没有添加到 href 的末尾。 ID 是从单选按钮中正确获取的,但不会附加 ID。
【解决方案2】:

你可以写如下逻辑:

在隐藏输入中创建 url,为县 ID 使用一些常量名称,为相应的锚标签使用相同的类名

<input class="COUNTY_ID" type="hidden" value="addDefaultPoints.action?countryId=COUNTY_ID&height=400&width=350">

<a class="COUNTY_ID" href="#"/>

为单选按钮编写更改事件:

$('input[name="radioBtn"]').change(function(){

     var radioVal = $(this).val();
     var url = $('input[class="COUNTY_ID"]').val();

     url = url.replace('COUNTY_ID',radioVal);

     $('a[class="COUNTY_ID"]').attr('href',url);
});

对其他链接也使用类似的逻辑......

【讨论】:

  • 谢谢,我会检查并尽快通知您。
猜你喜欢
  • 2021-07-02
  • 1970-01-01
  • 2015-07-17
  • 2021-04-09
  • 2023-04-03
  • 1970-01-01
  • 2020-05-17
  • 2020-03-19
  • 2022-01-07
相关资源
最近更新 更多