最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Radio button onclick insert value to href - Stack Overflow

programmeradmin2浏览0评论

I’m trying to insert the url from an onlick event in the the radio buttonsto the href link but it not working. Here’s I have so far.

<input type="radio" name="orderID" value="1"  onclick="javascript:document.getElementById('editBTN').href='forms/editForm.cfm?orderID ='&this.value">

<input type="radio" name=" orderID " value="2" onclick="javascript:document.getElementById('editBTN').href='forms/editForm.cfm?orderID ='&this.value">

Etc…

The link below href should change depending on which radio button is clicked.

<a href="" id="editBTN"> Edit Order</a>

The above script returns the current url with “localhost/myApp/0” at the end. If I remove this ('forms/editForm.cfm?orderID='&) from the radio button it correctly return the orderId.

I would like this result localhost/myApp/forms/editForm.cfm?orderID=1. Any suggestion would be greatly appreciated.

I’m trying to insert the url from an onlick event in the the radio buttonsto the href link but it not working. Here’s I have so far.

<input type="radio" name="orderID" value="1"  onclick="javascript:document.getElementById('editBTN').href='forms/editForm.cfm?orderID ='&this.value">

<input type="radio" name=" orderID " value="2" onclick="javascript:document.getElementById('editBTN').href='forms/editForm.cfm?orderID ='&this.value">

Etc…

The link below href should change depending on which radio button is clicked.

<a href="" id="editBTN"> Edit Order</a>

The above script returns the current url with “localhost/myApp/0” at the end. If I remove this ('forms/editForm.cfm?orderID='&) from the radio button it correctly return the orderId.

I would like this result localhost/myApp/forms/editForm.cfm?orderID=1. Any suggestion would be greatly appreciated.

Share Improve this question edited Jun 25, 2010 at 22:57 Rex M 144k34 gold badges291 silver badges315 bronze badges asked Jun 25, 2010 at 22:52 user281867user281867 5873 gold badges12 silver badges21 bronze badges 1
  • @user - I cleaned it up a bit, but not sure those URL code blocks at the end are exactly what they're supposed to be, can you fix them to exactly what you're seeing? (For future questions...use the 1010 button up top when you're code is selected for formatting :) – Nick Craver Commented Jun 25, 2010 at 22:55
Add a ment  | 

3 Answers 3

Reset to default 3

JavaScript uses + to concatenate strings, not &:

onclick="document.getElementById('editBTN').href=
         'forms/editForm.cfm?orderID ='+this.value"

(line break added for readability) should work.

You can lose the javascript: prefix, by the way.

You listed jquery as one of your question tags. I'd simply place the following script onto my page and attach the click event on the radio's to push their value into the appropriate button attribute.

$('input[name=orderID]').click(function(e) {

$('#editBTN').attr('href', 'forms/editForm.cfm?orderID ='+$(this).val());

});

I think the & before this.value should be a +

发布评论

评论列表(0)

  1. 暂无评论