Pass hidden field value using Jquery

£可爱£侵袭症+ 提交于 2019-12-08 04:28:10

问题


I have a normal hidden Input field where I am generating random string. I need that to be appended to the URL where I am trying to post data to another page.

I have done this and works pretty well.

url:'Upload.html?field1=newvalue',

This is my hidden input field

<input type="hidden" id="randomdirectory"/>

Now instead of newvalue in the query string I need to pass my random directory value.


回答1:


easily accomplished just do the following

var value = $("#randomdirectory").val();
url:'Upload.html?field1='+value ,

thats it ..




回答2:


Just give a name to the hidden field.

<input name="field1" type="hidden" id="randomdirectory" value="randomvalue"/>



回答3:


url: 'Upload.html?field1='+$("#randomdirectory").val()



回答4:


If I got your question right and the ID is randomly generated then you need something of this type:

url: 'Upload.html?field1=newvalue&field2=' + $('[type="hidden"]').val();



回答5:


Assuming that this is a parameter to an ajax call (as others already have), a slightly better practice is this:

url: 'Upload.html',
data: { field1: $('#randomdirectory').val() }

This will ensure that your query string is URL encoded properly (jQuery handles it for you).



来源:https://stackoverflow.com/questions/10144837/pass-hidden-field-value-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!