how to pass multiple text box values in javascript variable?

时间:2015-04-23 05:16:52

标签: javascript php html

<input type="hidden" name="email" value="Website_list">
<input type="hidden" name="name" value="John.Doe@foo.com">
<input type="hidden" name="fname" value="John">

<script type="text/javascript">
 var subscriber_details = {
email : 'John.Doe@foo.com',
name  : 'Website_List',
fname : 'John'
}

</script>

i want to pass the hidden value in Javascript variable in same manner "Subscriber_details" .

So what should i do?

5 个答案:

答案 0 :(得分:1)

<script type="text/javascript">
 var subscriber_details = {
   email : "'"+ document.getElementsByName("email").value +"'",
   name  : "'"+ document.getElementsByName("name").value +"'",
   fname : "'"+ document.getElementsByName("fname").value +"'"
}

</script>

答案 1 :(得分:0)

Assign id and some value to hidden input feilds:

html Code

<input type="hidden" name="email" id="email" value="my@email.com">
<input type="hidden" name="name"  id="name"  value="somename">
<input type="hidden" name="fname" id="fname" value="fname">

Then get it by id:

Javascript:

<script type="text/javascript">
  var email = document.getElementById("email");
  var name = document.getElementById("name");
  var fname = document.getElementById("fname");
  var subscriber_details = {
   email : email
   name  : name,
   fname : fname 
 }
</script>

答案 2 :(得分:0)

<script type="text/javascript">
 var subscriber_details = {
   email : $('input[name="email"]').val().trim(),
   name  : $('input[name="name"]').val().trim(),
   fname : $('input[name="fname"]').val().trim()
}
</script>

(if you are using jQuery). Sanjay posted a vanilla js version that would work.

答案 3 :(得分:0)

try this DEMO

html:

<input type="hidden" name="email" id="email" value="sample@gmail.com">
<input type="hidden" name="name" id="name" value="Kavin">
<input type="hidden" name="fname" id="fname" value="smk">

script:

$(document).ready(function(){
  var em = $("#email").val();
     var name = $("#name").val();
    var fn = $("#fname").val();
alert(em);alert(name);alert(fn);
  });

答案 4 :(得分:0)

function(){
  var email = $("input[name='email']").val();
  var name = $("input[name='name']").val();
  var fname = $("input[name='fname']").val();
 var subscriber_details = {
 email : email,
   name  : name,
   fname : fname
};
  console.log(email);
  console.log(name);
  console.log(fname);
}