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

javascript - Passing value from one field to another - Stack Overflow

programmeradmin2浏览0评论

I want to pass the value 9112232453 of one textfield to another.

I know I need Javascript for this but I don't know how to do it.

HTML

<form method = "post" action="">
  <input type="checkbox" name="phone" value="9112232453" onclick='some_func();' >
  <input type="text" name="phone" value="" id="phone">
  <input type="submit" name="Go">
</form> 

Then later, I want to use the value in my php.

I want to pass the value 9112232453 of one textfield to another.

I know I need Javascript for this but I don't know how to do it.

HTML

<form method = "post" action="">
  <input type="checkbox" name="phone" value="9112232453" onclick='some_func();' >
  <input type="text" name="phone" value="" id="phone">
  <input type="submit" name="Go">
</form> 

Then later, I want to use the value in my php.

Share Improve this question edited Sep 25, 2015 at 18:51 Buzinas 11.8k2 gold badges38 silver badges59 bronze badges asked Sep 25, 2015 at 18:45 user5377415user5377415 0
Add a ment  | 

4 Answers 4

Reset to default 3

You could use a JS. function to take param (this.value) like:

<script>       
 var some_func = function  (val){
            var input = document.getElementById("phone");
            input.value = val;
        }
</script>

<form method = "post" action="">
    <input type="checkbox" name="phone" value="9112232453" onclick='some_func(this.value);' >
    <input type="text" name="phone" value="" id="phone">
<input type="submit" name="Go">
    </form> 

The best way is to not obtrude the HTML code with Javascript event handlers.

So, you can add a DOMContentLoaded event listener to the document, and as soon as DOM is loaded:

  1. You add a change event listener to the input[type=checkbox], and then:

    1.1. If the checkbox is checked, then you change the input#phone's value to its value

    1.2. If not, then you empty the input#phone's value.

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById('cbphone').addEventListener('change', function(e) {
    var phone = document.getElementById('phone');
    if (this.checked) {
      phone.value = this.value;
      // you can even enable/disable the input#phone field, if you want to, e.g:
      // phone.disabled = false; 
    }
    else {
      phone.value = '';
      // phone.disabled = true; 
    }
  });
});
<form method="post" action="">
  <input type="checkbox" name="cbphone" id="cbphone" value="9112232453">
  <input type="text" name="phone" id="phone">
  <input type="submit" name="Go" value="Go">
</form>

before submit form use validation and check whether the field value is filled up or not. if yes get value of the field.

if(document.getElementBy("fieldIdfirst").value!="")
{
   document.getElementBy("fieldIdSecond").value=document.getElementElementById("fieldIdfirst");
}

Thanks it..

Try this: http://jsfiddle/yhuxy4e1/

HTML:

<form method = "post" action="">
    <input type="checkbox" name="phone" value="9112232453" onclick='some_func();' id="chk_phone">
    <input type="text" name="phone" value="" id="txt_phone">
    <input type="submit" name="Go">
</form>

JavaScript:

some_func = function() {
    var checkBox = document.getElementById('chk_phone');
    var textBox = document.getElementById('txt_phone');
    textBox.value = checkBox.value;
}
发布评论

评论列表(0)

  1. 暂无评论