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

Javascript - concatenate field value to onclick button link? - Stack Overflow

programmeradmin2浏览0评论

I have an issue I need to fix on an existing app that I didn't initially write. Here is a snippet of code that doesn't do what it is intended to do. What it is supposed to do is take the value of the field and upon clicking "Search", append that to the redirection to pass in the querystring to the destination page:

<form name="frm_someform">
    <input type="text" name="f_date" id="f_date"/>
    <input type="button" value="Search" onclick="parent.location='runreport.asp?date=' + document.frm_someform.elements['f_date'].value); + '"/>
</form>

Now, as you javascript folks can plainly see, the concatenation doesn't work. I've searched high and low for how to properly concatenate, but something isn't translating correctly (in my head). Note that if I take out the concatenation, the redirection works fine, so there is something with that causing the issue. Yes, of course in the example above, I could simply make the form submit the proper value with a real 'submit' button, but I have whittled the code down here for simplicity - it is much more plex than the example I have above.

(*Note, I successfully tested concatenation through other javascript functions, but the possibility exists that the purely inline code must be different)

Thanks in advance, Beems

I have an issue I need to fix on an existing app that I didn't initially write. Here is a snippet of code that doesn't do what it is intended to do. What it is supposed to do is take the value of the field and upon clicking "Search", append that to the redirection to pass in the querystring to the destination page:

<form name="frm_someform">
    <input type="text" name="f_date" id="f_date"/>
    <input type="button" value="Search" onclick="parent.location='runreport.asp?date=' + document.frm_someform.elements['f_date'].value); + '"/>
</form>

Now, as you javascript folks can plainly see, the concatenation doesn't work. I've searched high and low for how to properly concatenate, but something isn't translating correctly (in my head). Note that if I take out the concatenation, the redirection works fine, so there is something with that causing the issue. Yes, of course in the example above, I could simply make the form submit the proper value with a real 'submit' button, but I have whittled the code down here for simplicity - it is much more plex than the example I have above.

(*Note, I successfully tested concatenation through other javascript functions, but the possibility exists that the purely inline code must be different)

Thanks in advance, Beems

Share Improve this question asked Nov 14, 2012 at 23:54 BeemsBeems 8102 gold badges16 silver badges36 bronze badges 4
  • What happens when you try and output the document.frm_someform.elements['f_date'].value) in a browser developer console? (Say using console.log()?) Does it return the correct/expected value? – Fergus In London Commented Nov 14, 2012 at 23:58
  • 1 parent.location = 'runreport.asp?date=' + document.frm_someform.elements['f_date'].value); + ' What is parent? Why is there a closing parenthesis? Why is there a + and a ' after the semicolon? – m02ph3u5 Commented Nov 14, 2012 at 23:58
  • Fred cleaned it up for me. I had a number of extra non-necessary (and potentially detrimental) characters following the collection of the field value. – Beems Commented Nov 15, 2012 at 0:07
  • marabutt - I covered that above in the ments below the code block – Beems Commented Nov 15, 2012 at 0:08
Add a ment  | 

1 Answer 1

Reset to default 5

Please, try this:

<form name="frm_someform">
    <input type="text" name="f_date" id="f_date"/>
    <input type="button" value="Search" onclick="parent.location='runreport.asp?date='+ document.getElementById('f_date').value"/>
</form>
发布评论

评论列表(0)

  1. 暂无评论