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

php - get the input box 'name' using javascript - Stack Overflow

programmeradmin1浏览0评论

I am making form in PHP and submitting using javascript. I want to get the name of input box, As normal coding gives the value of the input box, but i need to get name of input box. Without onclick functionality on it. Can it possible that retrieving the name of input box in other javascript function on click a button. for example

<input type="radio" name="text_name" value="4" id="d">

Now onclick a other button I am calling test(), In test() how can I get the name of the input box.

I am making form in PHP and submitting using javascript. I want to get the name of input box, As normal coding gives the value of the input box, but i need to get name of input box. Without onclick functionality on it. Can it possible that retrieving the name of input box in other javascript function on click a button. for example

<input type="radio" name="text_name" value="4" id="d">

Now onclick a other button I am calling test(), In test() how can I get the name of the input box.

Share Improve this question edited Oct 11, 2012 at 6:02 Toon Krijthe 53.4k38 gold badges149 silver badges202 bronze badges asked Oct 11, 2012 at 5:59 Parth Sarthi GourParth Sarthi Gour 1151 gold badge2 silver badges5 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 13
var txtbox = document.getElementById("d");
var txtboxname = txtbox.name;

u can do that by using attr function...

in yout test function add this...

$('#d').attr('name');  // this will return text_name in your case (if you are using jquery)

OR

var inputname=document.getElementById("d").name  //javascript

put this on your javascript function

var name=document.getElementById("d").name;

function test() {
return document.getElementById("d").name; }

Not really sure what you are trying to do, but if your script is called from a listener on a button in the form, you can do:

<input type="button" value="Send form" onclick="doSubmit(this)">

Then the doSubmit function is something like:

function doSubmit(el) {
  var form = el.form;
  var control, controls = form.elements;

  for (var i=0, iLen=controls.length; i<iLen; i++) {
    control = controls[i];

    /*  
        Do stuff with control.
        Control name is control.name, control value is control.value
    */
  }
}

I would strongly recommend that your form has a standard submit button and works without any scripting, then you can add your scripted submission to the form's submit handler. It can cancel the standard submit and do its own thing.

That way if script is disabled, not available or fails for some reason, the user can still submit the form.

发布评论

评论列表(0)

  1. 暂无评论